Jump to content

Gwaihirjp

New Members
  • Posts

    7
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Female

Gwaihirjp's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Not at all. Use "ORDER BY ( yourField = 'TOYOTA' ) DESC, yourField ASC" Wow, this worked like magic!! I didn't know there was a way to order it like that. Thank you so much!
  2. Hi, I have a table in the database full of information of cars, and when I extract the info via PHP I want it ordered by company name but not exactly in alphebetical order. I need the company "TOYOTA" to be on top, and then all the other companies (Nissan, Honda, Chevrolet, etc) to come after it in alphabetical order. Only "TOYOTA" has to be prioritized and at the top. I know, it's a strange way to order soemthing, but is it possible? I looked up on how to use "order by field", but I'm not sure if that's the way to accomplish it. Is there an easier way to do it?
  3. Thank you thank you!!!!! Now it works perfectly! I added the lines like you said and replaced all the $s with $offset, and now it works just like I wanted it to. Here's the final code, in case someone comes with the same problem in the future. <?php $maker=$_GET['maker']; $model=$_GET['model']; $from=$_GET['from']; $to=$_GET['to']; $db=@mysql_connect("localhost","username","password"); if (!$db) {die("Unable to connect to database".mysql_error());} mysql_select_db("database") or die("Unable to select database"); //select which database we're using // rows to return $limit=10; if(isset($_GET['page'])) { $page=(int) $_GET['page']; } else { $page=1; } $query="select * from stocklist where manufacturer like '%$maker%' && name like '%$model%' && year>='$from' && year<='$to' order by year desc, name, stock_no"; if ($maker=="all" && $model==null) {$query="select * from stocklist where year>='$from' && year<='$to' order by year desc"; } if ($maker=="all" && $model!=null) {$query="select * from stocklist where name like '%$model%' && year>='$from' && year<='$to' order by year desc, name, stock_no"; } $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { die ("<br />Sorry, your search returned no results."); } $offset=($page-1)*$limit; $query.=" LIMIT $offset,$limit"; $result=mysql_query($query) or die ("couldn't reach query"); echo "$numrows matches found for your search "$maker $model $from ~ $to""; $color1="#ddffff"; $color2="#ffffff"; echo "<br /><br /><table cellspacing=/0/>"; $count=1+$offset; while ($row=mysql_fetch_array($result)) { $stockno=$row['1']; $year=$row['2']; $manufacturer=$row['4']; $modelname=$row['5']; $chassis=$row['6']; $trans=$row['7']; $color=$row['8']; $ps=$row['9']; $pw=$row['10']; $ac=$row['11']; $door=$row['12']; $seat=$row['13']; $grade=$row['14']; $other=$row['15']; $fuel=$row['16']; $cc=$row['17']; $price=$row['18']; $russia=$row['19']; $exterior=$row['20']; $interior=$row['21']; $availability=$row['22']; $rowcolor=($count % 2) ? $color1:$color2; echo "<tr height=\"30px\" bgcolor=\"$rowcolor\" nowrap> <td> $count.</td> <td>$stockno</td> <td>$year</td> <td>$manufacturer</td> <td>$modelname</td> <td>$chassis</td> <td>$trans</td> <td>$color</td> <td>$ps</td> <td>$pw</td> <td>$ac</td> <td>$door</td> <td>$seat</td> <td>$grade</td> <td>$other</td> <td>$availability</td> <td><a href=/#/><b>DETAILS</b></a></td> </tr>" ; $count++; }//end while $currpage=(($s/$limit)+1); echo "</table><br />"; if ($page>1) { // bypass PREV link if s is 0 $previous=$page-1; print "<a href=\"$PHP_SELF?maker=$maker&model=$model&from=$from&to=$to&page=$previous\"><<Prev 10</a> "; } $totalpages=ceil($numrows/$limit); if($page!=$totalpages) { $next=$page+1; print " <a href=\"$PHP_SELF?maker=$maker&model=$model&from=$from&to=$to&page=$next\">Next 10 >></a> "; } $a=$offset+($limit); if ($a>$numrows) {$a=$numrows;} $b=$offset+1; echo "Showing results $b to $a of $numrows"; ?> Thanks again!
  4. I followed your guideline and that tutorial (can't believe I hadn't looked there), and managed to get the links to move between the pages!! I can see the "next" and "prev" links in the right places. But, the table and its content still remains the same (Results 1 to 10) no matter what page it's on. Is it because I'm missing the "LIMIT $offset, $rowsperpage" in the query like it's written in the tutorial? <?php $maker=$_GET['maker']; $model=$_GET['model']; $from=$_GET['from']; $to=$_GET['to']; $db=@mysql_connect("localhost","username","password"); if (!$db) {die("Unable to connect to database".mysql_error());} mysql_select_db("database") or die("Unable to select database"); //select which database we're using // rows to return $limit=10; if(isset($_GET['page'])) { $page=(int) $_GET['page']; } else { $page=1; } $query="select * from stocklist where manufacturer like '%$maker%' && name like '%$model%' && year>='$from' && year<='$to' order by year desc, name, stock_no"; if ($maker=="all" && $model==null) {$query="select * from stocklist where year>='$from' && year<='$to' order by year desc"; } if ($maker=="all" && $model!=null) {$query="select * from stocklist where name like '%$model%' && year>='$from' && year<='$to' order by year desc, name, stock_no"; } $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { die ("<br />Sorry, your search returned no results."); } if (empty($s)) {$s=0;} $query.=" limit $s,$limit"; $result=mysql_query($query) or die ("couldn't reach query"); echo "Results for search "$maker $model""; $color1="#ddffff"; $color2="#ffffff"; echo "<br /><br /><table cellspacing=/0/>"; $count=1+$s; while ($row=mysql_fetch_array($result)) { $stockno=$row['1']; $year=$row['2']; $manufacturer=$row['4']; $modelname=$row['5']; $chassis=$row['6']; $trans=$row['7']; $color=$row['8']; $ps=$row['9']; $pw=$row['10']; $ac=$row['11']; $door=$row['12']; $seat=$row['13']; $grade=$row['14']; $other=$row['15']; $fuel=$row['16']; $cc=$row['17']; $price=$row['18']; $russia=$row['19']; $exterior=$row['20']; $interior=$row['21']; $availability=$row['22']; $rowcolor=($count % 2) ? $color1:$color2; echo "<tr height=\"30px\" bgcolor=\"$rowcolor\" nowrap> <td> $count.</td> <td>$stockno</td> <td>$year</td> <td>$manufacturer</td> <td>$modelname</td> <td>$chassis</td> <td>$trans</td> <td>$color</td> <td>$ps</td> <td>$pw</td> <td>$ac</td> <td>$door</td> <td>$seat</td> <td>$grade</td> <td>$other</td> <td>$availability</td> <td><a href=/#/><b>DETAILS</b></a></td> </tr>" ; $count++; }//end while $currpage=(($s/$limit)+1); echo "</table><br />"; if ($page>1) { // bypass PREV link if s is 0 $previous=$page-1; print "<a href=\"$PHP_SELF?maker=$maker&model=$model&from=$from&to=$to&page=$previous\">Prev 10</a> "; } $totalpages=ceil($numrows/$limit); if($page!=$totalpages) { $next=$page+1; print " <a href=\"$PHP_SELF?maker=$maker&model=$model&from=$from&to=$to&page=$next\">Next 10 >></a>"; } $a=$s+($limit); if ($a>$numrows) {$a=$numrows;} $b=$s+1; echo "Showing results $b to $a of $numrows"; ?>
  5. Yep - you perform the search each time the page loads so you need to know what your searching for everytime. Okay, I've changed the form a bit so that it's method="get" instead of "post" because I thought that maybe it might be easier. This is how it looks now: <?php $maker=$_GET['maker']; $model=$_GET['model']; $from=$_GET['from']; $to=$_GET['to']; // rows to return $limit=10; $db=@mysql_connect("localhost","username","password"); if (!$db) {die("Unable to connect to database".mysql_error());} mysql_select_db("database") or die("Unable to select database"); //select which database we're using $query="select * from stocklist where manufacturer like '%$maker%' && name like '%$model%' && year>='$from' && year<='$to' order by year desc, name, stock_no"; if ($maker=="all" && $model==null) {$query="select * from stocklist where year>='$from' && year<='$to' order by year desc"; } if ($maker=="all" && $model!=null) {$query="select * from stocklist where name like '%$model%' && year>='$from' && year<='$to' order by year desc, name, stock_no"; } $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { die ("<br />Sorry, your search returned no results."); } if (empty($s)) {$s=0;} $query.=" limit $s,$limit"; $result=mysql_query($query) or die ("couldn't reach query"); echo "Results for search "$maker $model""; $color1="#ffffff"; $color2="#ff00aa"; echo "<br /><br /><table cellspacing=/0/>"; $count=1+$s; while ($row=mysql_fetch_array($result)) { $stockno=$row['1']; $year=$row['2']; $manufacturer=$row['4']; $modelname=$row['5']; $chassis=$row['6']; $trans=$row['7']; $color=$row['8']; $ps=$row['9']; $pw=$row['10']; $ac=$row['11']; $door=$row['12']; $seat=$row['13']; $grade=$row['14']; $other=$row['15']; $fuel=$row['16']; $cc=$row['17']; $price=$row['18']; $russia=$row['19']; $exterior=$row['20']; $interior=$row['21']; $availability=$row['22']; $rowcolor=($count % 2) ? $color1:$color2; echo "<tr height=/30px/ bgcolor=/$rowcolor/ nowrap> <td> $count.</td> <td>$stockno</td> <td>$year</td> <td>$manufacturer</td> <td>$modelname</td> <td>$chassis</td> <td>$trans</td> <td>$color</td> <td>$ps</td> <td>$pw</td> <td>$ac</td> <td>$door</td> <td>$seat</td> <td>$grade</td> <td>$other</td> <td>$availability</td> <td><a href=/#/><b>DETAILS</b></a></td> </tr>" ; $count++; } $currpage=(($s/$limit)+1); echo "</table><br />"; if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print "<a href=\"$PHP_SELF?s=$prevs&maker=$maker&model=$model&from=$from&to=$to\">Prev 10</a> "; } $pages=ceil($numrows/$limit); if ($numrows%$limit) { $pages++; } if (!(($s+$limit)==$pages) && $pages!=1) { $news=$s+$limit; print " <a href=\"$PHP_SELF?s=$news&maker=$maker&model=$model&from=$from&to=$to\">Next 10 >></a>"; } $a=$s+($limit); if ($a>$numrows) {$a=$numrows;} $b=$s+1; echo "Showing results $b to $a of $numrows"; ?> But, now when I click on the "next" link, the exact same page with the exact same results comes back (as opposed to a page with no results) and leads nowhere. Furthermore, there's a new problem: the "next" button comes up even when the number of results is less than 10 and therefore unnecessary. It started happening when I replaced intval() with ceil(). Please help!
  6. Hi GingerRobot, thanks for your help! I think I see what you mean about the $_GET array. Just now I did $s=$_GET['s'] before the URL and replaced intval() with ceil(). Is there anything else I need to do? What would be the correct URL to for the "next" link if I want to pass all the search criteria to the next page? Do I need to do $_GET for "maker", "model" and years "from" & "to" as well? Sorry I'm asking so many questions.
  7. Hello everyone! I am a total noob here and this is my first post, so I'll introduce myself first. I'm from Japan and I design/program websites with HTML and CSS. But I've always wanted to learn how to use PHP/MySQL to build dynamic websites, and now I am trying to build my very first PHP webpage with a search form that returns data from a server. And, I think that I have been pretty successful so far considering how little I know. However, I have come to a point where I am completely stuck and desperately in need of help. The problem is this: The search results are paged (10 results per paged) and the first page works fine, but the links to the "prev" and "next" pages don't work. You see, the database contains details of many cars (from a car dealer), and my search form asks for the car's manufacturer (i.e.Toyota), model name, and year. The PHP code goes like this: $maker=$_POST['maker']; $model=$_POST['model']; $from=$_POST['from']; $to=$_POST['to']; // rows to return $limit=10; $db=@mysql_connect("localhost","username","password"); if (!$db) {die("Unable to connect to database".mysql_error());} mysql_select_db("database") or die("Unable to select database"); //select which database we're using $query="select * from table1 where manufacturer like '%$maker%' && name like '%$model%' && year>='$from' && year<='$to' order by year desc, name, stock_no"; if ($maker=="all" && $model==null) {$query="select * from stocklist where year>='$from' && year<='$to' order by year desc"; } if ($maker=="all" && $model!=null) {$query="select * from stocklist where name like '%$model%' && year>='$from' && year<='$to' order by year desc, name, stock_no"; } $numresults=mysql_query($query); $numrows=mysql_num_rows($numresults); if ($numrows == 0) { die ("<br />Sorry, your search returned no results."); } if (empty($s)) {$s=0;} $query.=" limit $s,$limit"; $result=mysql_query($query) or die ("couldn't reach query"); echo "Results for search "$maker $model""; $color1="#ffffff"; $color2="#ffffaa"; echo "<br /><br /><table cellspacing=/0/>"; $count=1+$s; while ($row=mysql_fetch_array($result)) { $stockno=$row['1']; $year=$row['2']; $manufacturer=$row['4']; $modelname=$row['5']; $chassis=$row['6']; $trans=$row['7']; $color=$row['8']; $ps=$row['9']; $pw=$row['10']; $ac=$row['11']; $door=$row['12']; $seat=$row['13']; $grade=$row['14']; $other=$row['15']; $fuel=$row['16']; $cc=$row['17']; $price=$row['18']; $russia=$row['19']; $exterior=$row['20']; $interior=$row['21']; $availability=$row['22']; $rowcolor=($count % 2) ? $color1:$color2; echo "<tr height=/30px/ bgcolor=/$rowcolor/ nowrap> <td> $count.</td> <td>$stockno</td> <td>$year</td> <td>$manufacturer</td> <td>$modelname</td> <td>$chassis</td> <td>$trans</td> <td>$color</td> <td>$ps</td> <td>$pw</td> <td>$ac</td> <td>$door</td> <td>$seat</td> <td>$grade</td> <td>$other</td> <td>$availability</td> <td><a href=/#/><b>DETAILS</b></a></td> </tr>" ; $count++; } $currpage=(($s/$limit)+1); echo "</table><br />"; if ($s>=1) { // bypass PREV link if s is 0 $prevs=($s-$limit); print "<a href=\"$PHP_SELF?s=$prevs&q=$maker\">Prev 5</a> "; } $pages=intval($numrows/$limit); if ($numrows%$limit) { $pages++; } if (!(($s+$limit)==$pages) && $pages!=1) { $news=$s+$limit; print " <a href=\"$PHP_SELF?s=$news&q=$maker\">Next 5 >></a>"; } $a=$s+($limit); if ($a>$numrows) {$a=$numrows;} $b=$s+1; echo "Showing results $b to $a of $numrows"; ?> I know this code must be pretty junky, but this really is my first time with PHP so please forgive me. I think that the last part must be garbled up and that's why the links lead nowhere but to the initial page without results. Please, can someone help me figure this out? I really want this paged results to work. Can anyone
×
×
  • 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.