AdRock Posted November 24, 2006 Share Posted November 24, 2006 I have some code which does my pagination but I only want it to be displayed if there is at least one record in the database. If there is no records then nothing is displayed....maybe a message displaying "no records exist"How do I do it so it checks the database to see if there is any records and if there are then display the records otherwise display a message?here is my code[code]h2>Delete a new article</h2><div><?php include "../includes/connection.php";$webpage = basename(delete_event); function pagination_five($total_pages,$page){ global $webpage; $max_links = 10; $h=1; if($page>$max_links){ $h=(($h+$page)-$max_links); } if($page>=1){ $max_links = $max_links+($page-1); } if($max_links>$total_pages){ $max_links=$total_pages+1; } echo '<div class="page_numbers"> <ul>'; if($page>"1"){ echo '<li class="current"><a href="/admin/'.$webpage.'/1">First</a></li> <li class="current"><a href="/admin/'.$webpage.'/'.($page-1).'">Prev</a></li> '; } if($total_pages!=1){ for ($i=$h;$i<$max_links;$i++){ if($i==$page){ echo '<li><a class="current">'.$i.'</a></li>'; } else{ echo '<li><a href="/admin/'.$webpage.'/'.$i.'">'.$i.'</a> </li>'; } } } if(($page >="1")&&($page!=$total_pages)){ echo '<li class="current"><a href="/admin/'.$webpage.'/'.($page+1).'">Next</a></li> <li class="current"><a href="/admin/'.$webpage.'/'.$total_pages.'">Last</a></li> '; } echo '</ul> </div> '; }$result = mysql_query("Select count(*) from events") or die (mysql_error()); $numrows = mysql_fetch_row($result); if(isset($_GET['id'])?$page = $_GET['id']:$page = 1); $entries_per_page = 10; $total_pages = ceil($numrows[0]/$entries_per_page); $offset = (($page * $entries_per_page) - $entries_per_page); //after we have $total_pages and $page, we can include the //pagination style wherever we want on the page. //so far there is no output from the script, so we could have //pagination before or after the pages results //before the results $result = mysql_query("SELECT * FROM events ORDER BY id DESC LIMIT $offset,$entries_per_page"); ?><fieldset><legend>Please select an event to delete</legend><table><?php while($code = mysql_fetch_array($result)) {?> <tr><td><input style="border:none;" type="radio" name="loc" onClick="confirmDelete('/admin/deleteevent/<? echo $code['id']; ?>')"></td> <td><?echo $code['title'];?></td> </tr> <?} // Closes While Loop?></table></fieldset><br><br><?php //or after the results pagination_five($total_pages,$page); ?></div>[/code] Quote Link to comment Share on other sites More sharing options...
jwk811 Posted November 24, 2006 Share Posted November 24, 2006 you could check to see if there is any records by doing this:[code]$sql = "SELECT something FROM something WHERE something= '$something'";[/code]This will find the record if it exists. You can now check how many rows had that record with mysql_affected_rows($sql). If it comes up 0 that means there was no record. If it comes up more than zero that means there is a record. Like this.[code]$sql = "SELECT something FROM something WHERE something= '$something'";$result = mysql_query(mysql_affected_rows($sql));if ($result == 0){// do something here if there is no records}if($result > 0){//do something here if there is records}[/code]i hope this is what you wanted i didnt bother reading through the code you hadgood luck! Quote Link to comment Share on other sites More sharing options...
AdRock Posted November 24, 2006 Author Share Posted November 24, 2006 What I need it to do is check if the database table is empty and if it is display "No records exist" but if there is at least one record carry on with the rest of the code Quote Link to comment Share on other sites More sharing options...
AdRock Posted November 24, 2006 Author Share Posted November 24, 2006 I am really confused about somethingI have this code but it makes no sense to me....surely it's the wrong way around[code]$result = mysql_query("Select count(*) from events") or die (mysql_error());$numrows = mysql_num_rows($result); if ($numrows =!0) { echo "No records exist!"; }else { if(isset($_GET['id'])?$page = $_GET['id']:$page = 1); $entries_per_page = 10; $total_pages = ceil($numrows[0]/$entries_per_page); $offset = (($page * $entries_per_page) - $entries_per_page); //after we have $total_pages and $page, we can include the //pagination style wherever we want on the page. //so far there is no output from the script, so we could have //pagination before or after the pages results //before the results $result = mysql_query("SELECT * FROM events ORDER BY id DESC LIMIT $offset,$entries_per_page"); ?><fieldset><legend>Please select an event to delete</legend><table><?php while($code = mysql_fetch_array($result)) {?> <tr><td><input style="border:none;" type="radio" name="loc" onClick="confirmDelete('/admin/deleteevent/<? echo $code['id']; ?>')"></td> <td><?echo $code['title'];?></td> </tr> <?} // Closes While Loop?></table></fieldset><br><br><?php //or after the results pagination_five($total_pages,$page); }[/code]i thought if $numrow is not equal to 0 then it should display the rest not ther error message Quote Link to comment Share on other sites More sharing options...
marcus Posted November 24, 2006 Share Posted November 24, 2006 You should do this:[code]if($numrows == 0){echo "error message";}else {//stuff};[/code] Quote Link to comment Share on other sites More sharing options...
marcus Posted November 24, 2006 Share Posted November 24, 2006 What you have is if $numrows DOESNT equal 0 it will display the message. Quote Link to comment Share on other sites More sharing options...
AdRock Posted November 24, 2006 Author Share Posted November 24, 2006 I have tried doing this[code]$result = mysql_query("Select count(*) from events") or die (mysql_error()); $numrows = mysql_num_rows($result);if ($numrows ==0){ echo "No records exist!";} else { echo "Records exist!";}[/code]but i get "Records exist" displayed even though I have nothing in the table. I have even dropped that table and created a new one with the same error Quote Link to comment Share on other sites More sharing options...
marcus Posted November 24, 2006 Share Posted November 24, 2006 Hmm.[code]$sql = "SELECT * FROM events";$res = mysql_query($sql);$num = mysql_numrows($res);if($num == 0){echo "none";}else {echo "some";};[/code] Quote Link to comment 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.