Skip navigation
Currently Being Moderated

displaying and echo in the body

Apr 27, 2012 5:38 AM

i have the echo no results being displayed but when its run  it currently displays at the top of the page

 

if(count($results) > 0){

         echo implode($results);

     }

     else {

         echo "No results were found.";

     }

 

 

how do i show the No results were found. in the contents

 

 

thanks in advance

 
Replies
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 5:49 AM   in reply to Jonathan Fortis

    Try this. Be sure to post the code where you want the result to be displayed

     

    $noresults = "No results were found!";

    if(count($results) > 0){

             echo implode($results);

         }

         else {

             echo "<p>$noresults</p>";

         }

     

     

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 6:04 AM   in reply to Sudarshan Thiagarajan

    This is because currently, I guess you're echoeing from within the PHP function code that you have all the way in the beginning of your code. This considers that the echo is a part of the function - hence it displays at the top of the page

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    Apr 27, 2012 6:57 AM   in reply to Jonathan Fortis

    You're welcome

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    May 4, 2012 4:52 AM   in reply to Jonathan Fortis

    Are you using an if else condition? If you don't PHP will display this even if there are results found.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    May 4, 2012 5:29 AM   in reply to Jonathan Fortis

    Post your full code so I'll know the location of your noresults code.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    May 4, 2012 6:16 AM   in reply to Jonathan Fortis

    I see you haven't defined the variable $results anywhere else in your code. The following PHP function tells the system to count your variable results and if the count is more than 0 in our case, it implodes the results into your table. If the count is not more than 0, then it echos the variable noresults which says 'No results were found'.

    <?php

            $noresults = "No results were found!";

    if(count($results) > 0){

             echo implode($results);

         }

         else {

             echo "<p>$noresults</p>";

         }

    ?>

     

    In your case, since you haven't defined the results variable, the PHP script just echos the noresults variable as it doesn't find the results variable anywhere in your code.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    May 5, 2012 9:55 PM   in reply to Jonathan Fortis

    You have to define $results as a variable before you add this code anywhere on your page. You should tell the system what value it should assume as $results.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    May 6, 2012 9:49 PM   in reply to Jonathan Fortis

    Sorry for the M.I.A. Personal emergency.

     

    results should be a variable before you can use that variable to manipulate data strings.

     

    For example,

     

    <?php
    $a
    [0] = 1;
    $a[1] = 3;
    $a[2] = 5;
    $result = count($a);
    // $result == 3
    ?>

     

    In this $result is defined as a total count of all $a values. This is static PHP. However, in your case, you should define $result to be the value of the actual results of your search query. For example, if you want to validate if your total rows returned from your sql query to be the markup for your PHP function, your code will be as follows:

     

    $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);

    $result = $totalRows_Recordset1

    In this, we define the totalRows in Recordset1 as the total number of all rows in Recordset1. We then use that value as the $result. If you run a count function here, you could say if count is >, return a message saying there are rows present in the table. Else, echo that there are no rows.

     

    Trust this helps.

     
    |
    Mark as:
  • Sudarshan Thiagarajan
    4,000 posts
    Oct 15, 2010
    Currently Being Moderated
    May 7, 2012 5:18 AM   in reply to Jonathan Fortis

    You have the answer to this in your question itself!

     

    Look at how you've configured newParams

     

    $queryString_Recordset1 = "";

    if (!empty($_SERVER['QUERY_STRING'])) {

      $params = explode("&", $_SERVER['QUERY_STRING']);

      $newParams = array();

      foreach ($params as $param) {

        if (stristr($param, "pageNum_Recordset1") == false &&

            stristr($param, "totalRows_Recordset1") == false) {

          array_push($newParams, $param);

        }

      }

      if (count($newParams) != 0) {

        $queryString_Recordset1 = "&" . htmlentities(implode("&", $newParams));

      }

    }

    You're first querying your DB and you've defined $newparams as an array. Then, you're running a validation on it to find out its count.

     

    Your $results variable should be configured in a very similar manner.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points