Jump to content

Problem with Query


miligraf

Recommended Posts

I got a TMS, if you go here [a href=\"http://www.miligraf.com/tutoriales/?pag=populares\" target=\"_blank\"]http://www.miligraf.com/tutoriales/?pag=populares[/a] the tuts are suppouse to be ordered descending, from the most clicked to the least clicked, but as you can see i get the most clicked at the bottom and then it starts off at the top.

heres the code for that one:

[code]<? include ('connection.php'); ?>
<?php

if(!isset($_GET['page'])){    
$page = 1;
}else{
$page = $_GET['page'];
}
$max_results = 6;                                        
$from = (($page * $max_results) - $max_results);

        $query = "SELECT * FROM tutoriales ORDER BY clics DESC LIMIT $from, $max_results";
        $result = mysql_query($query)
        or die("Error in query: $query . " . mysql_error());

        if(mysql_num_rows($result) > 0)
        {
            while($row = mysql_fetch_row($result))
            {
                $id = $row[0];
                $titulo = $row[1];
                $imagen = $row[2];
                   $clics = $row[3];
                $descripcion = $row[4];
                   $enlace = $row[5];
                $autor = $row[6];
                $fecha = $row[7];
            ?>
        <table width="100%" style='border-bottom: 1px dashed silver' cellpadding="0" cellspacing="0">
                <tr>
                    <td width="20%" rowspan="3" align="center" valign="middle"><a href="tutorial.php?id=<?php echo $id; ?>" target="_blank"><img src="<?php echo $imagen; ?>" alt="<?php echo $titulo; ?>" border="0"></a></td>
                  <td width="80%" height="24"><a href="tutorial.php?id=<?php echo $id; ?>" target="_blank"><?php echo "<span class='style20 Estilo1'> $titulo </span>"; ?></a></td>
                </tr>
                <tr>
                  <td><?php echo "<span class='style20 Estilo3'> $descripcion </span>"; ?></td>
              </tr>
                <tr>
                  <td height="24"><span class='style20 Estilo3'>Agregado el</span> <?php echo "<span class='style20 Estilo3'> $fecha </span>"; ?> <span class='style20 Estilo3'>por</span> <?php echo "<span class='style20 Estilo1'> $autor </span>"; ?> <span class='style20 Estilo3'>| Total de clics </span><?php echo "<span class='style20 Estilo1'> $clics </span>"; ?></td>
          </tr>
</table>
            <?php
            }
        }
        ?>
        <?php
            echo '<br><center>';

$total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM tutoriales"),0);

$total_pages = ceil($total_results/$max_results);
for($i = 1; $i <= $total_pages; $i++){
if(($page) == $i){
echo "<span class='style20 Estilo3'>$i</span>";
}else{                  
echo "<span class='style20 Estilo3'><a href='?page=$i'>$i</a></span>";  
} }
?>
<?php mysql_close($connection);?>[/code]

honestly, i dont see any problem in the code. it woked fine at the begining then it started showing up like this. ty for your help and patience!
Link to comment
Share on other sites

i give up. what's wrong with it? i stared at it for half an hour and it looks fine to me too [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

okay just out of curiosity, what datatype is your clics column?

i just clicked one of them a bunch of times (sorry) and when it passed 100 it now appears at the bottom too.
Link to comment
Share on other sites

most likely the "clics" column is a varchar, making it sort like strings, i.e., "100" sorts BEFORE (or after for desc) "2" because it starts with "1". you should change it to an INT data type, or dynamically convert clics to an int by adding "+0" after clics.

[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] * [color=green]FROM[/color] [color=orange]tutoriales[/color] [color=green]ORDER BY[/color] clics[color=orange]+[/color]0 [color=green]DESC[/color] LIMIT $from, $max_results [!--sql2--][/div][!--sql3--]
Link to comment
Share on other sites

lmao thanks for the clicks Crayon [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /] i see you were not so lost in what you suspected...sorry for taking 30 mins of your day [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

thank you ryanlwh ill change the clics column to INT.

pd. just did it and it works fine!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.