0

I want to create drop down menu where I can sort my product by price. Now I am trying this version, maybe it's not best idea, but if you have better please show.

    <h3>Mobilieji Telefonai</h3>
    <form method="post" action="">
     <select name="price">
    <option value="prioritetas">Atsitiktinis</option>
        <option value="kaina DESC">Kaina nuo mažiausios</option>
        <option value="kaina ASC">Kaina nuo didžiausios</option>
    </select>
    <input type="submit" name="orderPrice" value="orderPrice" />

 </form>
  </div>
  <?php


  if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
  $startrow = 0;
 } else {
   $startrow = (int)$_GET['startrow'];
    }
   $sort = @$_POST['price']; 
    $fetch = mysql_query("SELECT * FROM telefonai order by ".$sort." LIMIT $startrow, 15")or
 die(mysql_error());
 $fetch = mysql_query($query);
 print mysql_error();

 $num=Mysql_num_rows($fetch);
    if($num>0)
    {
    echo "<table border=2  >";
    echo "<tr><td>Telefono pavadinimas</td><td>Nuotrauka<td>Kaina</td>     <td>Parduotuve</td><td>Nuoroda</td></tr>";
    for($i=0;$i<$num;$i++)
    {
    $row=mysql_fetch_row($fetch);
    echo "<tr>";
    echo"<td>$row[1]</td>";
    echo "<td>   <img src=\"{$row[5]}\"  width=75  height=75/>    </td>";
    echo"<td>$row[2] LT</td>";
    echo"<td>$row[3]</td>";
    echo "<td><a href=\"{$row[4]}\"><img  src=\"".base_url()."images/parduotuve.png\" /></a></td>";
    echo"</tr>";
    }
    echo"</table>";
    }
  echo '<a href="'.base_url().$this->uri->segment(1)."/".'?startrow='.     ($startrow+5).'">Sekantis</a>';
 $prev = $startrow - 5;
 if ($prev >= 0)
    echo '<a href="'.base_url().$this->uri->segment(1)."/".'?startrow='.$prev.'">    Buves</a>';
  ?>
  </form>
  </body>
  </html>
  <br>

Nothing Clicked, just opened page

a busy cat

after click sorting

a busy cat

also with sorting not working table pagination, but this I will try to fix later

need to correct code and I want to fix this error. Any idea how to fix this error?

12
  • It looks like you are using $price without defining it Commented May 4, 2014 at 17:49
  • Could you echo the mysql query it might show if anything is wrong in that. But please be carefull with using POST in your query without validating it in anyway you would be wide open to attacks
    – melvin
    Commented May 4, 2014 at 17:50
  • a little bit not understand what you want from echo mysql query. You want to say that is something wrong with my database or table? echo was: Resource id #39
    – Nekasas
    Commented May 4, 2014 at 17:59
  • i mean the query you create "SELECT * FROM telefonai order by ".$price." LIMIT $startrow, 5" assign that to a var or something and echo it and with it i mean the query string itself not the result of it. it might show that certain values are empty or strings when you expect integers stuff like that
    – melvin
    Commented May 4, 2014 at 18:06
  • wihout drop down menu everything is working fine. You want that I will show what table I get?
    – Nekasas
    Commented May 4, 2014 at 18:10

3 Answers 3

1

I will try to formulate it as answer because i need to add the code.

Btw for this answer i assume your only problem is the sql error you have in your screenshot. The place you placed your print for the mysqlerror would never be reached btw because you have an mysql error and die() stops everything.

I think that the first answer is correct and you have to use $sort instead of $price. But if that does not work the code below should show you how to add extra debugging.

On a side note really use prepared statements or add some checking on sort (a white list or something)

This is the complete code (without validation) with some test code commented out. If it does not work after that you should uncomment the test code and run again and show the output. If things do work you can remove the commented lines i added.

  <h3>Mobilieji Telefonai</h3>
    <form method="post" action="">
     <select name="price">
    <option value="prioritetas">Atsitiktinis</option>
        <option value="kaina DESC">Kaina nuo mažiausios</option>
        <option value="kaina ASC">Kaina nuo didžiausios</option>
    </select>
    <input type="submit" name="orderPrice" value="orderPrice" />

 </form>
  </div>
  <?php


  if (!isset($_GET['startrow']) or !is_numeric($_GET['startrow'])) {
    $startrow = 0;
  } else {
    $startrow = (int)$_GET['startrow'];
  }
   $sort = @$_POST['price']; 

$query = "SELECT * FROM telefonai order by " . $sort . " LIMIT $startrow, 5";
//$query2 = "SELECT * FROM telefonai order by ". $price . " LIMIT $startrow, 5";
print $query;
//print $query2;

$fetch = mysql_query($query)or die(mysql_error());

$num=Mysql_num_rows($fetch);
    if($num>0)
    {
    echo "<table border=2  >";
    echo "<tr><td>Telefono pavadinimas</td><td>Nuotrauka<td>Kaina</td>     <td>Parduotuve</td><td>Nuoroda</td></tr>";
    for($i=0;$i<$num;$i++)
    {
    $row=mysql_fetch_row($fetch);
    echo "<tr>";
    echo"<td>$row[1]</td>";
    echo "<td>   <img src=\"{$row[5]}\"  width=75  height=75/>    </td>";
    echo"<td>$row[2] LT</td>";
    echo"<td>$row[3]</td>";
    echo "<td><a href=\"{$row[4]}\"><img  src=\"".base_url()."images/parduotuve.png\" /></a></td>";
    echo"</tr>";
    }
    echo"</table>";
    }
  echo '<a href="'.base_url().$this->uri->segment(1)."/".'?startrow='.     ($startrow+5).'">Sekantis</a>';
 $prev = $startrow - 5;
 if ($prev >= 0)
    echo '<a href="'.base_url().$this->uri->segment(1)."/".'?startrow='.$prev.'">    Buves</a>';
  ?>
  </form>
  </body>
  </html>
  <br>

Addition after more information

Depending on your expected functionality you can do 2 things.

1) If there is a default sorting order you should set that when price is empty

 $sorts = array('prioritetas', 'kaina ASC', 'kaina DESC');
 if ((!isset($_POST['price']) || !is_numeric($_POST['price'])) && !in_array($_POST['price'], $sorts)) {
    $sort = 'kaina ASC';
  } else {
     $sort = '$_POST['price']';
  }

2) OR if there is no default sort only add the sort to the query when price is not empty

 $sorts = array('prioritetas', 'kaina ASC', 'kaina DESC');
 $query = "SELECT * FROM telefonai";    
 if ((!isset($_POST['price']) || !is_numeric($_POST['price'])) && !in_array($_POST['price'], $sorts)) {
     $query .= "order by " . $sort;
 }
 $query .=  " LIMIT $startrow, 5";

I added an example of the white listing for you as well

3
  • no @ in this line? $sort = '$_POST['price']'; and tomorrow I will watch more your answers. And for me is only 1 your code fitting for me and very big thanks for trying to help me
    – Nekasas
    Commented May 4, 2014 at 20:08
  • No. you should try to avoid @ it suppress errors. When you supress errors you Will miss what goes wrong. It is best to check for this. In this case it was actually part of your problem. Probably you would have gotten a notice that your post was not set and so give a hint to solving your problem. Btw I see I placed a ' before and after $_POST['price'] they should not be there of course.
    – melvin
    Commented May 4, 2014 at 20:24
  • answer, if(isset($_POST['price'])) { $sort = $_POST['price']; } else { $sort = 'kaina ASC'; } and thanks for everything
    – Nekasas
    Commented May 6, 2014 at 15:36
1

Try to change:

$sort = $_POST['price']; 
$fetch = mysql_query("SELECT * FROM telefonai order by ".$price." LIMIT $startrow,5")

to:

$sort = $_POST['price']; 
$fetch = mysql_query("SELECT * FROM telefonai order by ".$sort." LIMIT $startrow, 5")

You are not defining the variable $price anywhere.

3
  • Photo was taking with $sort not $price and I dont know why I Coppied this.So, still error exist
    – Nekasas
    Commented May 4, 2014 at 17:53
  • thats a bad suggestion to use @ for suppressing the error/notice Commented May 4, 2014 at 18:28
  • what is better suggestion?
    – Nekasas
    Commented May 4, 2014 at 18:58
0

Change this:

      $sort = @$_POST['price']; 

To:

if(isset($_POST['price'])) {
$sort = $_POST['price'];
 } else {
 $sort = 'kaina ASC';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.