Jump to content

Only display if records in database exist


AdRock

Recommended Posts

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]
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 had
good luck!
I am really confused about something

I 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
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.