co.ador Posted December 11, 2009 Share Posted December 11, 2009 This is the error <b>Fatal error</b>: SQL in <b>C:\wamp\www\nyhungry\index3.php</b> on line <b>438</b><br /> Coming from this line <?php $sql = 'SELECT * FROM '. $arrRestaurants. ' LIMIT $offset, $rowsperpage'; $result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR); //line 438?> Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/ Share on other sites More sharing options...
mrMarcus Posted December 11, 2009 Share Posted December 11, 2009 change you argument in trigger_error() to: trigger_error(mysql_error()); and echo $sql: echo $sql; after your query to see if your query is what you think it is. EDIT: something tells me $arrRestaurants is going to be sloppy. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975482 Share on other sites More sharing options...
co.ador Posted December 11, 2009 Author Share Posted December 11, 2009 O Mr marcus I have tried to adapt the pagination script it's going so so. I have change the function you have given to me and now the results are Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$offset, $rowsperpage' at line 1 in C:\wamp\www\nyhungry\index3.php on line 450 Warning: Invalid argument supplied for foreach() in C:\wamp\www\nyhungry\index3.php on line 452 [1] 2 3 > >> <?php $sql = 'SELECT * FROM '. $arrRestaurants. ' LIMIT $offset, $rowsperpage'; $result = mysql_query($sql) or trigger_error(mysql_error()); // line 450 $i = 1; foreach($result as $arrRestaurant) { // line 452 ?> Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975487 Share on other sites More sharing options...
premiso Posted December 11, 2009 Share Posted December 11, 2009 EDIT: something tells me $arrRestaurants is going to be sloppy. I doubt it will be sloppy, I bet it is an array ($arr). So what is going on is he is not populating a string with the array data to form a statement for the SELECT clause. He is also not properly specifying a table name. My bet is he wants to use the $arrRest as the WHERE condition. Post the structure of your $arrRest array (do a print_r on the data and post it here within tags and perhaps we can get you a step further to understanding where you went wrong. A proper query structure: SELECT columns FROM table_name WHERE somecondition = another condition LIMIT 0, 10 Look at that and see where you are going wrong on your query. We do not know what is contained in $arrRest, so yea. Given that you have 450 lines+ in your script I bet it is an array and you are just confused on its usage. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975495 Share on other sites More sharing options...
mrMarcus Posted December 11, 2009 Share Posted December 11, 2009 EDIT: something tells me $arrRestaurants is going to be sloppy. I doubt it will be sloppy, I bet it is an array ($arr). it is an array. i remember this from yesterday. sloppy in the sense that there's no proper formatting on the array for it to be handled in the query. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975500 Share on other sites More sharing options...
co.ador Posted December 11, 2009 Author Share Posted December 11, 2009 I have done this way since there are one query already SELECTING those fields and it is contained in the $arrRestaurants variable. Will I have to open another query then or still used the one already in there It is an array. <pre>Array ( [0] => Array ( [restaurants_id] => 1 [restaurantname] => Choin Sun Express [image] => images/giacomos.jpg [description] => The first Joe's Shanghai [address] => 2483 Jerome Ave. ) [1] => Array ( [restaurants_id] => 3 [restaurantname] => Caridad at kingsbridge [image] => images/CaridadatKB.jpg [description] => aRestaurant critics, neighborhood regulars, [address] => 135-45 West Kingsbridge Rd. ) [2] => Array ( [restaurants_id] => 4 [restaurantname] => Giovanni's Restaurant [image] => images/giovannisRestaurant.jpg [description] => Tosca offers the irresistible flavors of Italian cuisine [address] => 80 West Fordham Road ) [3] => Array ( [restaurants_id] => 5 [restaurantname] => Yu Hua [image] => images/Restaurantsimages/yuhua.jpg [description] => The brilliant comfortable red interior inside YU HUA restaurant [address] => 83 W. Kingsbride Road ) [4] => Array ( [restaurants_id] => 6 [restaurantname] => N.1 Chinese food restaurant [image] => images/Restaurantsimages/n1restaurant.jpg [description] => N.1 Chinese restaurant at 64 w kingbridge rd, [address] => 64 w kingbridge rd, ) [5] => Array ( [restaurants_id] => 7 [restaurantname] => El Sabroso Mangu Restaurant [image] => images/sabrosomangu1.jpg [description] => Sabroso Mangu is a sport bar and restaurant [address] => 80 W Kinsgridge Rd. ) [6] => Array ( [restaurants_id] => 8 [restaurantname] => Nike Zoom Sharkalaid [image] => ../caridadimages/alitaspicantedepollo.gif [description] => [address] => ) Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975503 Share on other sites More sharing options...
premiso Posted December 11, 2009 Share Posted December 11, 2009 What is the goal of the query? Where do you want to pull data from? How do you want to pull it (like where restuarant id = x or what). Really that query is not a query at all, so please provide me with the information above so we can get you properly formed query. @mrMarcus: Gotcha Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975515 Share on other sites More sharing options...
nozai Posted December 11, 2009 Share Posted December 11, 2009 On top of you trying to use an array as a string, you're also using single quotes around your LIMIT statement. It's passing the literal variable names to MySQL instead of the variables' contents. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975521 Share on other sites More sharing options...
premiso Posted December 11, 2009 Share Posted December 11, 2009 you're also using single quotes around your LIMIT statement. Not really, take a closer look. He is concatenating and using the single quotes as the string specifier in PHP. The LIMIT is fine, the rest of the query is not. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975525 Share on other sites More sharing options...
co.ador Posted December 11, 2009 Author Share Posted December 11, 2009 The goal of the query was to pull the data from the $arrRestaurants variable LIMIT $rowsperpage = 3, $offset = ($currentpage - 1) * $rowsperpage'; Ok The $arrRestaurants variable is coming from <?php $strSQL = sprintf( 'SELECT r.restaurants_id ,r.restaurantname ,r.image ,r.description ,r.address ,r.zip ,r.state ,r.restaurants_id FROM restaurants r %s %s %s' ,$boolIncludeZipCodes === true?'INNER JOIN restaurants_to_zip_codes rz ON r.restaurants_id = rz.restaurants_id ':'' ,empty($arrSQLFilters)?'':' WHERE '.implode(' AND ',$arrSQLFilters) ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':'' ); // Run search query $arrResult = mysql_query($strSQL) or die("Cannot execute:". mysql_error()); while($arrRow = mysql_fetch_assoc($arrResult)) { $arrRestaurants[] = $arrRow; } ?> $arrRestaurants[] array variable is at the bottom of the above script at the while loop, Then I wanted to take that variable $arrRestaurants which right now it's used to display the fields according to the conditions of the query above. The html frame to display the fields above is set up as, //else if grab the variable $arrRestaurants <?php else if (!empty($arrRestaurants)){ ?> // Html Frame <div id="holders"><img src="images/smallholdersleft.gif" alt="holders" /></div> <!--<dvi id="holders1"><img src="images/holders1.gif" alt="holders" /></div>--> <div id="header2"> <fieldset class="header2field"><img src="images/Restaurantsresults.gif" alt="Restaurants Results"/></fieldset> </div> <div id="container4"> <div class="wrap"> if the else if statement meet that conditions then <?php $i = 1; foreach($arrRestaurants as $arrRestaurant) { echo "<div class=\"shoeinfo1\"> <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" /> <h2 class=\"infohead\">". $arrRestaurant['restaurantname'] . "</h2> <div class=\"pic\"><img class=\"line\" src= ". $arrRestaurant['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div> <h5> Rating:</h5><h4>"; $ratingData = Rating::OutputRating($arrRestaurant['restaurantname']); if (Error::HasErrors()) { echo Error::ShowErrorMessages(); Error::ClearErrors(); } else { echo $ratingData; } echo"</h4> <h3>Description:</h3> <div id=\"description\"><p>".$arrRestaurant['description']." </p></div> <div class=\"suabe2\">Address:<span class=\"suabe\">".$arrRestaurant['address']."</span></div> <div id=\"state\">State:<span class=\"suabe\">". $arrRestaurant['state']. "</span></div> <h6>Zip:<span class=\"suabe\">". $arrRestaurant['zip'] . "</span></h6> <p><a href=\"#\">More</a></p></div> "; $i++; if ($i > 1 && $i % 4 == 0 ) { echo "<div class=\"clearer\"></div>"; } } } ?> Then that html frame is going to repeat as many rows the database has in it.... Well in my intent to adapt the php basic pagination tutorial I have form the queries the tutorial has but the second query I have wanted to use the $arrRestaurants variable so I don't have to build another query calling the same table, Plus it is dificult to use fit the html frame using a different query conditions and fitting the php basic pagination tutorial inside together with the existing html frame previous to adapting the pagination tutorial. The question would be how can I properly fit it.. This is what I have tried to fit the pagination script inside the existing script. <?php // Build search query and embed filters $strSQL = sprintf( 'SELECT r.restaurants_id ,r.restaurantname ,r.image ,r.description ,r.address ,r.zip ,r.state ,r.restaurants_id FROM restaurants r %s %s %s' ,$boolIncludeZipCodes === true?'INNER JOIN restaurants_to_zip_codes rz ON r.restaurants_id = rz.restaurants_id ':'' ,empty($arrSQLFilters)?'':' WHERE '.implode(' AND ',$arrSQLFilters) ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':'' ); // Run search query $arrResult = mysql_query($strSQL) or die("Cannot execute:". mysql_error()); while($arrRow = mysql_fetch_assoc($arrResult)) { $arrRestaurants[] = $arrRow; } else if (!empty($arrRestaurants)){ ?> <div id="holders"><img src="images/smallholdersleft.gif" alt="holders" /></div> <!--<dvi id="holders1"><img src="images/holders1.gif" alt="holders" /></div>--> <div id="header2"> <fieldset class="header2field"><img src="images/Restaurantsresults.gif" alt="Restaurants Results"/></fieldset> </div> <div id="container4"> <div class="wrap"> <?php // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM restaurants"; $result = mysql_query($sql) or trigger_error(mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 3; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $sql = 'SELECT * FROM '. $arrRestaurants. ' LIMIT $offset, $rowsperpage'; $result = mysql_query($sql) or trigger_error(mysql_error()." ".$sql); $i = 1; foreach($result as $arrRestaurant) { echo "<div class=\"shoeinfo1\"> <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" /> <h2 class=\"infohead\">". $arrRestaurant['restaurantname'] . "</h2> <div class=\"pic\"><img class=\"line\" src= ". $arrRestaurant['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div> <h5> Rating:</h5><h4>"; $ratingData = Rating::OutputRating($arrRestaurant['restaurantname']); if (Error::HasErrors()) { echo Error::ShowErrorMessages(); Error::ClearErrors(); } else { echo $ratingData; } echo"</h4> <h3>Description:</h3> <div id=\"description\"><p>".$arrRestaurant['description']." </p></div> <div class=\"suabe2\">Address:<span class=\"suabe\">".$arrRestaurant['address']."</span></div> <div id=\"state\">State:<span class=\"suabe\">". $arrRestaurant['state']. "</span></div> <h6>Zip:<span class=\"suabe\">". $arrRestaurant['zip'] . "</span></h6> <p><a href=\"#\">More</a></p></div> "; $i++; if ($i > 1 && $i % 4 == 0 ) { echo "<div class=\"clearer\"></div>"; } } $range = 2; // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ } ?> </div> </div> In the above script I use $arrRestaurants variable coming from the while loop below the $strSQL variable containing the query at the top of the script then, I test weather the $arrRestaurants variable is empty and if not empty then fit the first part of the tutorial with the name of the tables changed <?php // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM restaurants"; $result = mysql_query($sql) or trigger_error(mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 3; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; ?> Then for the second query used in the script I was trying to sloopily place the $arrRestaurants array variable inside the second query in the tutorial, since the $arrRestaurants variable contain the same fields I want to populate in the html frame below the second query which is the same html frame I have before trying to adapt the php basic pagination tutorial I have decided to use the $arrRestaurants inside the query without expecting any sucess just to see if it works. Second query and the html frame to display the fields <?php $sql = 'SELECT * FROM '. $arrRestaurants. ' LIMIT $offset, $rowsperpage'; $result = mysql_query($sql) or trigger_error(mysql_error()." ".$sql); $i = 1; foreach($result as $arrRestaurant) { echo "<div class=\"shoeinfo1\"> <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" /> <h2 class=\"infohead\">". $arrRestaurant['restaurantname'] . "</h2> <div class=\"pic\"><img class=\"line\" src= ". $arrRestaurant['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div> <h5> Rating:</h5><h4>"; $ratingData = Rating::OutputRating($arrRestaurant['restaurantname']); if (Error::HasErrors()) { echo Error::ShowErrorMessages(); Error::ClearErrors(); } else { echo $ratingData; } echo"</h4> <h3>Description:</h3> <div id=\"description\"><p>".$arrRestaurant['description']." </p></div> <div class=\"suabe2\">Address:<span class=\"suabe\">".$arrRestaurant['address']."</span></div> <div id=\"state\">State:<span class=\"suabe\">". $arrRestaurant['state']. "</span></div> <h6>Zip:<span class=\"suabe\">". $arrRestaurant['zip'] . "</span></h6> <p><a href=\"#\">More</a></p></div> "; $i++; if ($i > 1 && $i % 4 == 0 ) { echo "<div class=\"clearer\"></div>"; } } ?> Then I have put the second par of the tutorial which populate a number of pages according to the amount of rows populated from the database <?php $range = 2; // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if /****** end build pagination links ******/ } ?> Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975527 Share on other sites More sharing options...
co.ador Posted December 11, 2009 Author Share Posted December 11, 2009 I saw the variable after the LIMIT clouse and I thought I could do the same after the FROM clouse but I didn't know that and array variable can't be used in a query, or at least in the way I have set it up. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975528 Share on other sites More sharing options...
nozai Posted December 11, 2009 Share Posted December 11, 2009 Not really, take a closer look. He is concatenating and using the single quotes as the string specifier in PHP. The LIMIT is fine, the rest of the query is not. But, if you look at the SQL error: Notice: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$offset, $rowsperpage' you see that '$offset' and '$rowsperpage' are being passed as SQL. I am assuming that they are PHP variables, and if so, they are not being used properly. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975548 Share on other sites More sharing options...
premiso Posted December 11, 2009 Share Posted December 11, 2009 you see that '$offset' and '$rowsperpage' are being passed as SQL. I am assuming that they are PHP variables, and if so, they are not being used properly. I stand corrected, I see what you were getting at Sorry about that. @OP, yea you cannot use queries to pull data from arrays. You either need to create a function in PHP to loop through the array to do what you want or pull the data from another query to do the pagination. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975556 Share on other sites More sharing options...
co.ador Posted December 11, 2009 Author Share Posted December 11, 2009 Nozai It said SQL becuase the Trigger error function was set up like: <?php $result = mysql_query($sql) or trigger_error("SQL", E_USER_ERROR);?> Instead of <?php $result = mysql_query($sql) or trigger_error(mysql_error());?> After I have set it up like $result = mysql_query($sql) or trigger_error(mysql_error()); then the errors display as Notice: Unknown column 'r.restaurants_id' in 'field list' in C:\wamp\www\nyhungry\index3.php on line 452 Warning: Invalid argument supplied for foreach() in C:\wamp\www\nyhungry\index3.php on line 455 [1] 2 3 4 > >> Now I know I can't uset queries to pull up values from an array.. The only reason I am using the variable $arrRestaurats inside the query is because the population of the html frame fields are going to depend on the query conditions contained in the arrRestaurants. <?php $strSQL = sprintf( 'SELECT r.restaurants_id ,r.restaurantname ,r.image ,r.description ,r.address ,r.zip ,r.state FROM restaurants r %s %s %s' ,$boolIncludeZipCodes === true?'INNER JOIN restaurants_to_zip_codes rz ON r.restaurants_id = rz.restaurants_id ':'' ,empty($arrSQLFilters)?'':' WHERE '.implode(' AND ',$arrSQLFilters) ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':'' ); // Run search query $arrResult = mysql_query($strSQL) or die("Cannot execute:". mysql_error()); while($arrRow = mysql_fetch_assoc($arrResult)) { $arrRestaurants[] = $arrRow; } } ?> The above script with it's query is all conatain in the $arrRestaurants variable and those conditions inside this variable will be in charge of populating the data. how can I still use those conditions and arrenge the php basic paginations around the html frame so that every 3 rows users need to click on next such as below [1] 2 3 4 > >> The only thing populating is the numbers of the paginations but then the content inside the html frame is not displaying becuase of the queries interference or comflict. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975558 Share on other sites More sharing options...
nozai Posted December 11, 2009 Share Posted December 11, 2009 The only reason I am using the variable $arrRestaurats inside the query is because the population of the html frame fields are going to depend on the query conditions contained in the arrRestaurants. ... The above script with it's query is all conatain in the $arrRestaurants variable and those conditions inside this variable will be in charge of populating the data. It looks as though $arrRestaurants contains the data that you wish to paginate (not the conditions themselves). If this is the case, try: $arrDisplay = array_slice($arrRestaurants,$offset,$rowsperpage) and then use a "foreach" to format the data for display... Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975580 Share on other sites More sharing options...
co.ador Posted December 11, 2009 Author Share Posted December 11, 2009 Nozai that was the solution bro <?php $arrDisplay = array_slice($arrRestaurants,$offset,$rowsperpage); $i = 1; foreach($arrDisplay as $arrRestaurant) { echo "<div class=\"shoeinfo1\"> <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" /> <h2 class=\"infohead\">". $arrRestaurant['restaurantname'] . "</h2> <div class=\"pic\"><img class=\"line\" src= ". $arrRestaurant['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div> ?> ?> I just populated and then took out the query and mysql_query function.. still have to do some fixes such as echoing or displaying the next set of row [2] within the html frame, right now it is displaying the index.php page or $_SERVER['PHP_SELF']. I have the form submition action to action="<?php echo $_SERVER['PHP_SELF']; ?>" and the pagination pages [1] 2 3 > >> to $_SERVER['PHP_SELF'] as well. Some how the form values are not pick up by the pagination script to continue paginate according to form values. Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975585 Share on other sites More sharing options...
co.ador Posted December 11, 2009 Author Share Posted December 11, 2009 in case you didn't undertand <?php // Build search query and embed filters $strSQL = sprintf( 'SELECT r.restaurants_id ,r.restaurantname ,r.image ,r.description ,r.address ,r.zip ,r.state FROM restaurants r %s %s %s' ,$boolIncludeZipCodes === true?'INNER JOIN restaurants_to_zip_codes rz ON r.restaurants_id = rz.restaurants_id ':'' ,empty($arrSQLFilters)?'':' WHERE '.implode(' AND ',$arrSQLFilters) ,$boolIncludeZipCodes === true?'GROUP BY r.restaurants_id':'' ); // Run search query $arrResult = mysql_query($strSQL) or die("Cannot execute:". mysql_error()); while($arrRow = mysql_fetch_assoc($arrResult)) { $arrRestaurants[] = $arrRow; } } ?> <div id="wrapper"> <form name="frmSearch" class="abajo" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> <fieldset class="primero"> <legend class="primerosub">Search For a Restaurant</legend> <fieldset class="segundo1" style="border-color:#FFFFFF" > <legend>Location Details</legend> <ol> <li class="restaurants-name"> <label for="restaurants-name">Name</label> <input type="text" name="frmSearch[name]" value="<?php echo $strName; ?>" id="restaurants-name" class="text" /> </li> <li class="restaurants-zipcode"> <label for="restaurants-zipcode">Zip</label> <input type="text" name="frmSearch[zipcode]" value="<?php echo $strZipCode; ?>" maxlength="5" id="restaurants-zipcode" class="text" /> </li> <?php if(!empty($arrStates)) { ?> <li class="restaurants-state"> <label for="restaurants-state">State</label> <select name="frmSearch[state]" id="restaurants-state" style="width:40px;"> <?php foreach($arrStates as $strStateAbb) { printf( '<option value="%s"%s>%s</option>' ,$strStateAbb ,strcmp($strState,$strStateAbb) == 0?' selected="selected"':'' ,strcmp($strStateAbb,'')==0?'--':$strStateAbb ); } ?> </select> </li> <?php } ?> <?php if(!empty($arrRestaurantsFoodTypes)) { ?> <li class="restaurants-food-types"> <label for="restaurants-food-types">Food Type</label> <select name="frmSearch[food_types][]" id="restaurants-food-types"> <?php foreach($arrRestaurantsFoodTypes['values'] as $intIndex=>$intFoodTypesId) { printf( '<option value="%s"%s>%s</option>' ,$intFoodTypesId ,in_array($intFoodTypesId,$arrFoodTypes)?' selected="selected"':'' ,$arrRestaurantsFoodTypes['output'][$intIndex] ); } ?> </select> </li> <?php } ?> <ol class="resetsearch"> <li> <input type="submit" value="Submit" name="frmSearch[submit]"> </li> <li> <input type="submit" value="reset" name="frmSearch[reset]"> </li> </ol> </ol> </fieldset> <fieldset class="tercero" style=""> <legend>Services</legend> <?php if(!empty($arrRestaurantsOfferings)) { ?> <ol> <li class="restaurants-offerings"> <?php foreach($arrRestaurantsOfferings as $arrRestaurantsOffering) { printf( '<input type="checkbox" name="frmSearch[offerings][]" value="%u" id="restaurants-offerings-%u"%s /> <span class="checkboxes23">%s</span><br />' ,$arrRestaurantsOffering['restaurant_offerings_id'] ,$arrRestaurantsOffering['restaurant_offerings_id'] ,in_array($arrRestaurantsOffering['restaurant_offerings_id'],$arrOfferings)?' checked="checked"':'' ,$arrRestaurantsOffering['name'] ); } ?> </li> </ol> <?php } ?> </fieldset> </fieldset> </form> </div> <div id="videowrapper"> <fieldset class="videofield"> <object width="380" height="265"> <param name="movie" value="videoplayer5.swf"> <embed src="videos/vaideoplayer5.swf" width="380" height="240"> </embed> </object> </fieldset> </div> <?php if(empty($arrRestaurants)){ echo '<div id="holders"><img src="images/smallholdersleft.gif" alt="holders" /></div> <!--<dvi id="holders1"><img src="images/holders1.gif" alt="holders" /></div>--> <div id="header2"> <fieldset class="header2field"><img src="images/RecommendedRestaurants.gif" alt="Recommended Restaurants"/></fieldset> </div> <div id="container4"> <div class="wrap"> '; $query = "SELECT restaurants.state, restaurants.address ,restaurants.zip, restaurants.description, restaurants.restaurantname, restaurants.image, restaurants.restaurants_id FROM restaurants WHERE restaurants_id IN ( 1, 2, 3, 4)"; $result = mysql_query($query, $connection); $i = 1; while ($content = mysql_fetch_array($result)) { echo "<div class=\"shoeinfo1\"> <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" /> <h2 class=\"infohead\">". $content['restaurantname'] . "</h2> <div class=\"pic\"><img class=\"line\" src= ". $content['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div> <h5> Rating:</h5><h4>"; $ratingData = Rating::OutputRating($content['restaurantname']); if (Error::HasErrors()) { echo Error::ShowErrorMessages(); Error::ClearErrors(); } else { echo $ratingData; } echo"</h4> <h3>Description:</h3> <div id=\"description\"><p>".$content['description']." </p></div> <div class=\"suabe2\">Address:<span class=\"suabe\">".$content['address']."</span></div> <div id=\"state\">State:<span class=\"suabe\">". $content['state']. "</span></div> <h6>Zip:<span class=\"suabe\">". $content['zip'] . "</span></h6> <p><a href=\"#\">More</a></p></div> "; $i++; if ($i > 1 && $i % 4 == 0 ) { echo "<div class=\"clearer\"></div>"; } } echo '</div> </div>'; } else if (!empty($arrRestaurants)){ ?> <div id="holders"><img src="images/smallholdersleft.gif" alt="holders" /></div> <!--<dvi id="holders1"><img src="images/holders1.gif" alt="holders" /></div>--> <div id="header2"> <fieldset class="header2field"><img src="images/Restaurantsresults.gif" alt="Restaurants Results"/></fieldset> </div> <div id="container4"> <div class="wrap"> <?php // print search query //if(!empty($strSQL)) { //printf('<p>%s</p>',$strSQL); // find out how many rows are in the table $sql = "SELECT COUNT(*) FROM restaurants"; $result = mysql_query($sql) or trigger_error(mysql_error()); $r = mysql_fetch_row($result); $numrows = $r[0]; // number of rows to show per page $rowsperpage = 6; // find out total pages $totalpages = ceil($numrows / $rowsperpage); // get the current page or set a default if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { // cast var as int $currentpage = (int) $_GET['currentpage']; } else { // default page num $currentpage = 1; } // end if // if current page is greater than total pages... if ($currentpage > $totalpages) { // set current page to last page $currentpage = $totalpages; } // end if // if current page is less than first page... if ($currentpage < 1) { // set current page to first page $currentpage = 1; } // end if // the offset of the list, based on current page $offset = ($currentpage - 1) * $rowsperpage; // get the info from the db $arrDisplay = array_slice($arrRestaurants,$offset,$rowsperpage); $i = 1; foreach($arrDisplay as $arrRestaurant) { echo "<div class=\"shoeinfo1\"> <img src=\"images/spacer.gif\" alt=\"spacer\" class=\"spacer2\" /> <h2 class=\"infohead\">". $arrRestaurant['restaurantname'] . "</h2> <div class=\"pic\"><img class=\"line\" src= ". $arrRestaurant['image'] ." alt=\"picture\" width=\"100%\" height=\"100%\" /></div> <h5> Rating:</h5><h4>"; $ratingData = Rating::OutputRating($arrRestaurant['restaurantname']); if (Error::HasErrors()) { echo Error::ShowErrorMessages(); Error::ClearErrors(); } else { echo $ratingData; } echo"</h4> <h3>Description:</h3> <div id=\"description\"><p>".$arrRestaurant['description']." </p></div> <div class=\"suabe2\">Address:<span class=\"suabe\">".$arrRestaurant['address']."</span></div> <div id=\"state\">State:<span class=\"suabe\">". $arrRestaurant['state']. "</span></div> <h6>Zip:<span class=\"suabe\">". $arrRestaurant['zip'] . "</span></h6> <p><a href=\"#\">More</a></p></div> "; $i++; if ($i > 1 && $i % 4 == 0 ) { echo "<div class=\"clearer\"></div>"; } } /****** build the pagination links ******/ // range of num links to show $range = 2; // if not on page 1, don't show back links if ($currentpage > 1) { // show << link to go back to page 1 echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> "; // get previous page num $prevpage = $currentpage - 1; // show < link to go back to 1 page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> "; } // end if // loop to show links to range of pages around current page for ($x = ($currentpage - $range); $x < (($currentpage + $range) + 1); $x++) { // if it's a valid page number... if (($x > 0) && ($x <= $totalpages)) { // if we're on current page... if ($x == $currentpage) { // 'highlight' it but don't make a link echo " [<b>$x</b>] "; // if not current page... } else { // make it a link echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> "; } // end else } // end if } // end for // if not on last page, show forward and last page links if ($currentpage != $totalpages) { // get next page $nextpage = $currentpage + 1; // echo forward link for next page echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>></a> "; // echo forward link for lastpage echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>>></a> "; } // end if } ?> </div> </div> above is the query with the conditions, the form and the pagination script mixed with the html frame. If you notice the form action is set up to <?php echo $_SERVER['PHP_SELF']; ?> and the php basic pagination script is set to that as well. So when I click in the second page of rows in the pagination numbers below the set of rows inside the html frame then it comes back to the same page but not with the second set of rows but. The overall structure is 1- Query where fields are pick up 2- Form 3- if (empty($arrRestaurants)) { } else if (!empty($arrRestaurants)){ {php basic pagination} foreach(condition) { html frame } { Rest of the php basic pagination code where $_SERVER['PHP_SELF'] } Quote Link to comment https://forums.phpfreaks.com/topic/184781-having-an-php-database-error/#findComment-975596 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.