Hi, Daniweb

I'm working on a website where i did a search system and then i listed the results founded. And now i want to move them with JQuery Sortable.

But i'm having dificults can someone help me please?

Here's the code:

<?php 
    require_once('Connections/futura.php'); 
?>

<?PHP
    $prod=$_GET['q2'];

    $query_prod="SELECT * FROM produtos WHERE nome LIKE '%".$prod."%'";
    $produt=mysql_query($query_prod, $futura);
    $count=mysql_num_rows($produt);   


    $prod_img="SELECT * FROM produtos WHERE nome LIKE '%". $prod ."%' ORDER BY referencia ASC";
    $image=mysql_query($prod_img, $futura);
?>
<!----------------------------------------------------------------------------------------------------------------------->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/style.css" rel="stylesheet" />
<title>Plásticos Futura - Pesquisa</title>
<link rel="shortcut icon" type="image/x-icon" href="imagens/plasticos.ico" />
<script type="text/javascript" src="js/java.js"></script>

<!---------------------------------------------------------------------------------------------------->

<link type="text/css" href="jquery/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="jquery/jquery-1.3.2.js"></script>
<script type="text/javascript" src="jquery/ui.core.js"></script>
<script type="text/javascript" src="jquery/ui.sortable.js"></script>
<link type="text/css" href="jquery/demos.css" rel="stylesheet" />


<style type="text/css">
        #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
        #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
        #sortable li span { position: absolute; margin-left: -1.3em; }
</style>


<script type="text/javascript">
    $(function() {
        $("#sortable").sortable();
        $("#sortable").disableSelection();
    });
</script>
<!---------------------------------------------------------------------------------------------------->  



<table width="570" border="0" cellspacing="0" cellpadding="0" >

                <?PHP
                        $i=0;
                        while ($linha = mysql_fetch_array($image))
                        {
                        ?>

                        <?PHP
                            if ($i==0)
                            {
                                echo '<tr class="sortable_row" id="456">';
                            }
                            ?>

                            <?PHP
                                //MATERIAL
                                    $query_categ_list = "SELECT * FROM materiais WHERE id_material='".$linha['id_material']."'";
                                    $categ_list = mysql_query($query_categ_list, $futura) or die(mysql_error());
                                    $row_categ_list = mysql_fetch_assoc($categ_list);
                                    $totalRows_categ_list = mysql_num_rows($categ_list);
                                ?> <td colspan="2" class="div_topo"><a name="cat<?php echo $row_categ_list['id_material']; ?>"></a>&nbsp; <strong>PRODUTOS :: <span class="texto2"><?php echo $row_categ_list['material']; ?></span></strong></td>

                                <?PHP
                                //IMAGEM
                                $sel_img="SELECT * FROM  produtos_img WHERE id_produto = '".$linha['id_produto']."'";
                                $img2=mysql_query($sel_img, $futura);
                                $img = mysql_fetch_assoc($img2);
                                ?>
                                <tr>
                                <td width="160" height="95" valign="top"><a href="ver_produto.php?id_produto=<?php echo $linha['id_produto']; ?>&amp;id_categoria=<?php echo $linha['id_material']; ?>"><?php if($img['imagem']!="") { ?><img src="http://www.plasticos-futura.com/timthumb.php?src=http://www.plasticos-futura.com/produtos/<?php echo $img['imagem']; ?>&w=134px&h=98px&zc=1&q=100" width="134" height="98" style="border:1px solid #cccccc;" /><?php } else { ?><img src="imagens/futura.jpg" width="134" height="98" style="border:1px solid #cccccc;" /><?php } ?></a></td>

                                <td>
                                    <table>
                                        <tr>
                                            <td align="left">
                                                <strong>DESCRIÇÃO: </strong><?PHP echo $linha['nome'];?>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td align="left">
                                                <strong>REFERÊNCIA: </strong><?PHP echo $linha['referencia']; ?>
                                            </td>
                                        </tr>
                                    </table>
                                </td>

                                </tr>
                            <?PHP
                            $i=$i+1;
                        }
                ?>
        </table>
<?PHP
    }
?>
<!----------------------------------------------------------------------------------------------------------------------->
 </div></td>
        <td width="178"></td>
      </tr>
    </table></td>

</html>

Hope you can help me.

Thank you,
PF2G

Recommended Answers

All 13 Replies

Is it possible transform the table into a list, i think it's easier to manage the sortable.

Please i really need this.

Thank You ,
PF2G

I did it but i only can move image and material seperated can you help me moving imagem/description and the material at the same time?

<?php 
    require_once('Connections/futura.php'); 
?>

<?PHP
    $prod=$_GET['q2'];

    $query_prod="SELECT * FROM produtos WHERE nome LIKE '%".$prod."%'";
    $produt=mysql_query($query_prod, $futura);
    $count=mysql_num_rows($produt);   


    $prod_img="SELECT * FROM produtos WHERE nome LIKE '%". $prod ."%' ORDER BY referencia ASC";
    $image=mysql_query($prod_img, $futura);
?>
<!----------------------------------------------------------------------------------------------------------------------->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="styles/style.css" rel="stylesheet" />
<title>Plásticos Futura - Pesquisa</title>
<link rel="shortcut icon" type="image/x-icon" href="imagens/plasticos.ico" />
<script type="text/javascript" src="js/java.js"></script>

<!-------------------------------------------------------------------------------------->
<link rel="stylesheet" href="jquery/themes/base/jquery.ui.all.css">
<script src="jquery/jquery-1.7.2.js"></script>
<script src="jquery/ui/jquery.ui.core.js"></script>
<script src="jquery/ui/jquery.ui.widget.js"></script>
<script src="jquery/ui/jquery.ui.mouse.js"></script>
<script src="jquery/ui/jquery.ui.sortable.js"></script>
<link rel="stylesheet" href="jquery/demos.css">


<style type="text/css">
        #sortable 
        {
            list-style-type: none; margin: 0; padding: 0; width: 100%; 
        }


        #sortable tr 
        { 
            margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; height: 20px; 
        }
</style>

<script type="text/javascript">
    $(function() {
        $("#sortable").sortable();
        $("#sortable").disableSelection();
    });
</script>

<!---------------------------------------------------------------------------------------------------->  
</head>

<body>
<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" style="background-image:url(imagens/fundo5.jpg);background-repeat:repeat-y;">
<tr>
<td>
<table width="1200" border="0" align="center" cellpadding="0" cellspacing="0" style="background-image:url(imagens/fundo3.jpg); background-repeat:no-repeat;" height="713" >
  <tr>
    <td height="60" valign="bottom"><?php include("topo.php"); ?></td>
  </tr>
  <tr>
    <td height="613" valign="top"><table width="1200" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td valign="top"><table width="260" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td align="right"><a href="index.php" title="Voltar à homepage"><img src="imagens/plasticos-futura.png" border="0" /></a></td>
          </tr>
          <tr>
            <td height="40"></td>
          </tr>
          <tr>
            <td><table width="250" border="0" align="right" cellpadding="0" cellspacing="0">
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='index.php'"><a href="index.php" title="Home - Plásticos Futura" class="menu_text">Home</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='empresa.php'"><a href="empresa.php" class="menu_text" title="Empresa - Plásticos Futura">Empresa</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='produtos.php'"><a href="produtos.php" class="menu_text" title="Produtos - Plásticos Futura">Produtos</a></td>
              </tr>
              <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='noticias.php'"><a href="noticias.php" class="menu_text"  title="Notícias - Plásticos Futura">Notícias</a></td>
              </tr>
               <tr>
                <td height="2"></td>
                <td height="2"></td>
              </tr>
              <tr>
                  <td width="30"></td>
                  <td height="26" class="menu" onclick="window.location='oportunidades-de-negocio.php'"><a href="oportunidades-de-negocio.php" class="menu_text"  title="Oportunidades de Negócio - Plásticos Futura">Oportun. Negócio</a></td>
                </tr>
                <tr>
                  <td height="2"></td>
                  <td height="2"></td>
                </tr>
              <tr>
                  <td width="30"></td>
                  <td height="26" class="menu" onclick="window.location='recrutamento.php'"><a href="recrutamento.php" class="menu_text"  title="Recrutamento - Plásticos Futura">Recrutamento</a></td>
                </tr>
                <tr>
                  <td height="2"></td>
                  <td height="2"></td>
                </tr>
              <tr>
                <td width="30"></td>
                <td height="26" class="menu" onclick="window.location='contactos.php'"><a href="contactos.php" class="menu_text"  title="Contactos - Plásticos Futura">Contactos</a></td>
              </tr>
            </table></td>
          </tr>
        </table></td>


<!----------------------------------------------------------------------------------------------------------------------->   
         <td width="638" valign="top" class="texto_pretobig2"><div class="bomfundo"><strong>RESULTADOS DA PESQUISA::</strong><br />
        <br/>

<?PHP
        if($count==0)
        {
?>

        <strong>Não foram encontrados resultados!</strong>

<?PHP
        }   
        else
        { 
            echo 'Encontrado(s) '.$count.' resultado(s) de pesquisa com a palavra "'. $prod .'":';
?>
            <br/>
            <br/>
                <div class="demos">
                 <table id="sortable" width="570" border="0" cellspacing="0" cellpadding="0" >

                <?PHP
                        $i=0;
                        while ($linha = mysql_fetch_array($image))
                        {
                        ?>

                        <?PHP
                            if ($i==0)
                            {
                                echo '<tr>';
                            }
                            ?>
                            <thead>
                            <?PHP
                                //MATERIAL
                                    $query_categ_list = "SELECT * FROM materiais WHERE id_material='".$linha['id_material']."'";
                                    $categ_list = mysql_query($query_categ_list, $futura) or die(mysql_error());
                                    $row_categ_list = mysql_fetch_assoc($categ_list);
                                    $totalRows_categ_list = mysql_num_rows($categ_list);
                                ?> <td colspan="2" class="div_topo"><a name="cat<?php echo $row_categ_list['id_material']; ?>"></a>&nbsp; <strong>PRODUTOS :: <span class="texto2"><?php echo $row_categ_list['material']; ?></span></strong></td>
                            </thead>
                            <tbody>
                                <?PHP
                                //IMAGEM
                                $sel_img="SELECT * FROM  produtos_img WHERE id_produto = '".$linha['id_produto']."'";
                                $img2=mysql_query($sel_img, $futura);
                                $img = mysql_fetch_assoc($img2);
                                ?>
                                <tr>
                                <td width="160" height="95" valign="top"><a href="ver_produto.php?id_produto=<?php echo $linha['id_produto']; ?>&amp;id_categoria=<?php echo $linha['id_material']; ?>"><?php if($img['imagem']!="") { ?><img src="http://www.plasticos-futura.com/timthumb.php?src=http://www.plasticos-futura.com/produtos/<?php echo $img['imagem']; ?>&w=134px&h=98px&zc=1&q=100" width="134" height="98" style="border:1px solid #cccccc;" /><?php } else { ?><img src="imagens/futura.jpg" width="134" height="98" style="border:1px solid #cccccc;" /><?php } ?></a></td>

                                <td>
                                    <table>
                                        <tr >
                                            <td align="left">
                                                <strong>DESCRIÇÃO: </strong><?PHP echo $linha['nome'];?>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td align="left">
                                                <strong>REFERÊNCIA: </strong><?PHP echo $linha['referencia']; ?>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                                </tr>

                            </tbody>
                            <?PHP
                            $i=$i+1;
                        }
                ?>
        </table>
        </div>
<?PHP
    }
?>
<!----------------------------------------------------------------------------------------------------------------------->
 </div></td>
        <td width="178"></td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td height="40"><?php include("rodape.php"); ?></td>
  </tr>
</table>
</td>
  </tr>
  </table>
</body>
</html>

Appears you haven't specified what to sort... Where is #sortable ?

I solved the problem :)

Now i have a question if you can help me. My new task is saving the order that i sort, right?

And i have searched for codes and i didn't find anything sprecific, can you help me with some exemples or tell what i have to do?

My table is "produtos" and i know one thing: i have to update the column "ordem".

Can you help me, please?

Thank you
PF2G

Is these code alright, it gives me error in the "foreach":

<?PHP
// handle POST
if (isset($_POST['do_submit']))
{
    // use $i to increment the weight
    $i=1;
    // loop through post array in the order it was submitted
    foreach ($image['nome'] as $produt) 
    {
        // update the row
        $result = mysql_query("UPDATE produtos SET ordem=". $i . " WHERE id_produto=". mysql_real_escape_string($produt));
        // increment weight to make the next fruit heavier
        $i++;
    }
}
?>

<script type="text/javascript" src="jquery-1.2.6.js"></script>
<script type="text/javascript" src="jquery-ui-1.5.1.min.js"></script>

<script type="text/javascript">
    // when the entire document has loaded, run the code inside this function
    $(document).ready(function()
    {
        // Wow! .. One line of code to make the unordered list drag/sortable!
        $('#fruit_list').sortable();
    });
</script>


(...)

<form method="POST" action="">
                 <table id="sortable" width="570" border="0" cellspacing="0" cellpadding="0" >

                <?PHP
                        $i=0;
                        while ($linha = mysql_fetch_array($image))
                        {
                ?>

                <?PHP
                            if ($i==0)
                            {
                                echo '<tr>';
                            }
                ?>
                        <tbody>
                <?PHP
                            //MATERIAL
                                    $query_categ_list = "SELECT * FROM materiais WHERE id_material='".$linha['id_material']."'";
                                    $categ_list = mysql_query($query_categ_list, $futura) or die(mysql_error());
                                    $row_categ_list = mysql_fetch_assoc($categ_list);
                                    $totalRows_categ_list = mysql_num_rows($categ_list);
                ?> 
                <td colspan="2" class="div_topo"><a name="cat<?php echo $row_categ_list['id_material']; ?>"></a>&nbsp; <strong>PRODUTOS :: <span class="texto2"><?php echo $row_categ_list['material']; ?></span></strong></td>

                <?PHP
                                //IMAGEM
                                $sel_img="SELECT * FROM  produtos_img WHERE id_produto = '".$linha['id_produto']."'";
                                $img2=mysql_query($sel_img, $futura);
                                $img = mysql_fetch_assoc($img2);
                ?>
                                <tr>
                                <td width="160" height="95" valign="top"><a href="ver_produto.php?id_produto=<?php echo $linha['id_produto']; ?>&amp;id_categoria=<?php echo $linha['id_material']; ?>"><?php if($img['imagem']!="") { ?><img src="http://www.plasticos-futura.com/timthumb.php?src=http://www.plasticos-futura.com/produtos/<?php echo $img['imagem']; ?>&w=134px&h=98px&zc=1&q=100" width="134" height="98" style="border:1px solid #cccccc;" /><?php } else { ?><img src="imagens/futura.jpg" width="134" height="98" style="border:1px solid #cccccc;" /><?php } ?></a></td>

                                <td>
                                    <table>
                                        <tr >
                                            <td align="left">
                                                <strong>DESCRIÇÃO: </strong><?PHP echo $linha['nome'];?>
                                            </td>
                                        </tr>

                                        <tr>
                                            <td align="left">
                                                <strong>REFERÊNCIA: </strong><?PHP echo $linha['referencia']; ?>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                                </tr>
                            </tbody>
                        <?PHP
                            $i=$i+1;
                        }
                ?>
                </table>
                <br/>
            <input type="submit" name="do_submit" value="Submit Sortation" class="button" />
        </form>

Is there someone who can help me?

Yeah i did this already but i'm having dificults replacing the names you know? if i replace for the name that you search it gives me error. What do i replace here:

$menu = $_POST['menu'];

If you could help me a little bit more, i appreciated... :P

Thank you,
PF2G

I think this is ok but it gives me error:

Warning: Invalid argument supplied for foreach() in

Help me, please.

$_POST['your_variable_naem_here'] where the variable, is what you specified in the serialize. This script does not get called on submit, but as soon as some index changes. (I do not respond to PM's, keep it in the thread.)

My question is what is that variable, that is what is confusing me...

The one you serialize, and send in the update event. Didn't you call that fieldOrder?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.