Results 1 to 7 of 7

Thread: Create a message that says "no records found" with php

  1. #1
    Join Date
    Dec 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Create a message that says "no records found" with php

    Hello,

    I have the following code and it works with a page that has a search form with it. However when a user types in information that is not found in the mysql database I would like the page to return a message that says "no records found, please try your search again" I have tried several different options with no luck, is there anyone who can help me out (let me know what the code might be and where I would place it)?

    Thank you

    <?php

    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

    $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }
    }

    $maxRows_rsvendor = 10;
    $pageNum_rsvendor = 0;
    if (isset($_GET['pageNum_rsvendor'])) {
    $pageNum_rsvendor = $_GET['pageNum_rsvendor'];
    }
    $startRow_rsvendor = $pageNum_rsvendor * $maxRows_rsvendor;

    $varzip_rsvendor = "1";
    if (isset($_POST['Zip'])) {
    $varzip_rsvendor = $_POST['Zip'];
    }
    $varcategory_rsvendor = "1";
    if (isset($_POST['Category'])) {
    $varcategory_rsvendor = $_POST['Category'];
    }
    mysql_select_db($database_vendor, $vendor);
    $query_rsvendor = sprintf("SELECT commercial.Company, commercial.Street, commercial.City, commercial.Zip, commercial.Category FROM commercial WHERE commercial.Zip=%s AND commercial.Category=%s", GetSQLValueString($varzip_rsvendor, "text"),GetSQLValueString($varcategory_rsvendor, "text"));
    $query_limit_rsvendor = sprintf("%s LIMIT %d, %d", $query_rsvendor, $startRow_rsvendor, $maxRows_rsvendor);
    $rsvendor = mysql_query($query_limit_rsvendor, $vendor) or die(mysql_error());
    $row_rsvendor = mysql_fetch_assoc($rsvendor);

    if (isset($_GET['totalRows_rsvendor'])) {

    $totalRows_rsvendor = $_GET['totalRows_rsvendor'];

    } else {
    $all_rsvendor = mysql_query($query_rsvendor);
    $totalRows_rsvendor = mysql_num_rows($all_rsvendor);
    }

    $totalPages_rsvendor = ceil($totalRows_rsvendor/$maxRows_rsvendor)-1;
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <p>Commercial Vendors</p>
    <p>&nbsp;</p>
    <table border="0">

    <?php do { ?>

    <tr>
    <td width="157"><?php echo $row_rsvendor['Company']; ?></td></tr>
    <tr><td><?php echo $row_rsvendor['Street']; ?></td></tr>
    <tr><td><?php echo $row_rsvendor['City']; ?></td></tr>
    <tr><td><?php echo $row_rsvendor['Zip']; ?></td></tr>
    <tr><td><?php echo $row_rsvendor['Category']; ?></td>
    </tr>
    <tr>
    <td bgcolor="#666666">&nbsp;</td>
    </tr>
    <?php

    } while ($row_rsvendor = mysql_fetch_assoc($rsvendor));
    ?>
    </table>
    <?php
    mysql_free_result($rsvendor);
    ?>

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    In your case you can send a message back to the browser if there is no record in your recordset using the 'print' language construct in PHP and if there is no record the only thing going to the browser will be this message. The alogorithm is as follows:
    Code:
    if(recordset is empty){
    	print "<div>There are no records found</div>";
    }else{
    	Your main record processing stuff goes here
    }
    The second approach is using PHP we send a client-side script segment into the browser, which makes an alert saying that there are no records. The code will look like the following:

    Code:
    print "<script type='text/javascript'>alert('Records not found; Try another search');</script>";
    You can use any one of the above mentioned options.

  3. #3
    Join Date
    Dec 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you, however I must have some kind of block in my brain because I tried your first option in several different places in the code and continue to get an error. Is there anyway you can copy the sample code I placed above and show me right where this code needs to be?

    Thank you so much

  4. #4
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    PHP Code:
    <?php

    if (!function_exists("GetSQLValueString")) {
    function 
    GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue "")
    {
    $theValue get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

    $theValue function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

    switch (
    $theType) {
    case 
    "text":
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
    break;
    case 
    "long":
    case 
    "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case 
    "double":
    $theValue = ($theValue != "") ? "'" doubleval($theValue) . "'" "NULL";
    break;
    case 
    "date":
    $theValue = ($theValue != "") ? "'" $theValue "'" "NULL";
    break;
    case 
    "defined":
    $theValue = ($theValue != "") ? $theDefinedValue $theNotDefinedValue;
    break;
    }
    return 
    $theValue;
    }
    }

    $maxRows_rsvendor 10;
    $pageNum_rsvendor 0;
    if (isset(
    $_GET['pageNum_rsvendor'])) {
    $pageNum_rsvendor $_GET['pageNum_rsvendor'];
    }
    $startRow_rsvendor $pageNum_rsvendor $maxRows_rsvendor;

    $varzip_rsvendor "1";
    if (isset(
    $_POST['Zip'])) {
    $varzip_rsvendor $_POST['Zip'];
    }
    $varcategory_rsvendor "1";
    if (isset(
    $_POST['Category'])) {
    $varcategory_rsvendor $_POST['Category'];
    }
    mysql_select_db($database_vendor$vendor);
    $query_rsvendor sprintf("SELECT commercial.Company, commercial.Street, commercial.City, commercial.Zip, commercial.Category FROM commercial WHERE commercial.Zip=%s AND commercial.Category=%s"GetSQLValueString($varzip_rsvendor"text"),GetSQLValueString($varcategory_rsvendor"text"));
    $query_limit_rsvendor sprintf("%s LIMIT %d, %d"$query_rsvendor$startRow_rsvendor$maxRows_rsvendor);
    $rsvendor mysql_query($query_limit_rsvendor$vendor) or die(mysql_error());
    $row_rsvendor mysql_fetch_assoc($rsvendor);

    if (isset(
    $_GET['totalRows_rsvendor'])) {

    $totalRows_rsvendor $_GET['totalRows_rsvendor'];

    } else {
    $all_rsvendor mysql_query($query_rsvendor);
    $totalRows_rsvendor mysql_num_rows($all_rsvendor);
    }

    $totalPages_rsvendor ceil($totalRows_rsvendor/$maxRows_rsvendor)-1;
    ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Untitled Document</title>
    </head>

    <body>
    <p>Commercial Vendors</p>
    <p>&nbsp;</p>
    <table border="0">

    <?php
    if (mysql_num_rows($rsvendor) == 0) {
    echo 
    '<p>No records found.</p>';
    } else {
    do { 
    ?>

    <tr>
    <td width="157"><?php echo $row_rsvendor['Company']; ?></td></tr>
    <tr><td><?php echo $row_rsvendor['Street']; ?></td></tr>
    <tr><td><?php echo $row_rsvendor['City']; ?></td></tr>
    <tr><td><?php echo $row_rsvendor['Zip']; ?></td></tr>
    <tr><td><?php echo $row_rsvendor['Category']; ?></td>
    </tr>
    <tr>
    <td bgcolor="#666666">&nbsp;</td>
    </tr>
    <?php

    } while ($row_rsvendor mysql_fetch_assoc($rsvendor)); }
    ?>
    </table>
    <?php
    mysql_free_result
    ($rsvendor);
    ?>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  5. #5
    Join Date
    Dec 2007
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thank you very much

  6. #6
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default

    Will this work for the first row? It seems that $row_rsvendor is assigned after it gets done the first iteration.

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    This line:
    $row_rsvendor = mysql_fetch_assoc($rsvendor);
    Immediately after the mysql query, though not in the while loop, both gives the impression that there are results if there are not (as it isn't conditional, so that variable is set to something, even if that something is an error), and also offsets the start of the while loop then by one by placing the pointer of that resource one row ahead, meaning one iteration ahead.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •