Jump to content


Photo

Problem with Query


  • Please log in to reply
4 replies to this topic

#1 miligraf

miligraf
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 09 May 2006 - 09:57 PM

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:

<? 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);?>

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!

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 May 2006 - 10:34 PM

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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 10 May 2006 - 12:20 AM

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] * FROM tutoriales ORDER BY clics+0 DESC LIMIT $from, $max_results [!--sql2--][/div][!--sql3--]
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 10 May 2006 - 12:31 AM

yeh that's what i suspected too, ryanlwh.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#5 miligraf

miligraf
  • Members
  • PipPipPip
  • Advanced Member
  • 74 posts

Posted 10 May 2006 - 01:23 AM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users