Jump to content

Archived

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

87dave87

displaying $num_rows before rows appear?

Recommended Posts

Hi,  I have a search results page and I want to display $num_rows (so I can put Found 'x' downloads), but my code which has If statements from when it is started is below where I want the $num_rows to appear.

e.g. http://www.emulators.cc/  -  type 'atari' into the search box at the top and click go... this will display the results but I can't get the 'emulators found' part to work.

This is because im not declaring the sql query first, although ive tried to do this...

The code for the 'found 'x' emulators is:-

[code]
<?php
$database="database"; 
mysql_connect ("localhost", "user", "pass"); 
@mysql_select_db($database) or die( "Unable to select database");
$num_rows = mysql_num_rows($sql);
echo "<p class='footer'>"; 
echo $num_rows;
echo " emulators found</p>"; 
?>
[/code]

Code for the results to be displayed is: -

[code]
        <?php
if(isset($_POST["emusearch"]) && strlen(trim($_POST["emusearch"])) > 0){
    $database="database"; 
    mysql_connect ("localhost", "user", "pass"); 
    @mysql_select_db($database) or die( "Unable to select database"); 
    $sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc"; 
    if(!$rs = mysql_query($sql)){ 
        echo "Error<br>".mysql_error()."<br>".$sql; 
            exit(); 
  }
    if(!mysql_num_rows($rs)){ 
        echo "<tr>"; 
echo "<meta http-equiv='refresh' content='0; URL=http://www.google.com/foundnorecordspage'>";
        echo "</tr>\n"; 
    }else{ 
        while ($get_info = mysql_fetch_row($rs)) {   
            echo "<tr>";   
            foreach ($get_info as $field) echo "<td>$field</td>\n"; 
            echo "</tr>\n"; 
        } 
    } 
}else{
    echo "<meta http-equiv='refresh' content='0; URL=http://www.google.com/searchcriteriarequired'>";
}
?>
[/code]

Share this post


Link to post
Share on other sites
You still need a query... Do you want it to show the total in emulators, or just the total found once searched?

Regards
Huggie

Share this post


Link to post
Share on other sites
total found.

So if some types in 'stella', at the moment that gives 1, so it will display '1 emulator found'

Share this post


Link to post
Share on other sites
ok, modified from your code above, give this a try:

[code]<?php
if (isset($_POST['emusearch']) && strlen(trim($_POST['emusearch'])) > 0){
  $database="database"; 
  mysql_connect ("localhost", "user", "pass"); 
  @mysql_select_db($database) or die( "Unable to select database"); 
  $sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc";

  if(!$rs = mysql_query($sql)){ 
      echo "Error<br>".mysql_error()."<br>".$sql; 
      exit(); 
  }

  if(!mysql_num_rows($rs)){ 
      echo "<tr>";
      echo "<meta http-equiv='refresh' content='0; URL=http://www.google.com/foundnorecordspage'>";
      echo "</tr>\n"; 
  }
  else{
      $num_rows = mysql_num_rows($rs);
      echo "$num_rows results found<br>\n<br>\n";
      while ($get_info = mysql_fetch_row($rs)) {   
        echo "<tr>";   
        foreach ($get_info as $field){
            echo "<td>$field</td>\n"; 
        }
            echo "</tr>\n";   
      } 
  }
}
else{
    echo "<meta http-equiv='refresh' content='0; URL=http://www.google.com/searchcriteriarequired'>";
}
?>[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
Sorry, typo on my part.  I corrected it now, can you copy and paste and try again.

Regards
Huggie

Share this post


Link to post
Share on other sites
it works, but as the bar is outside of my result code, I can't do anything with it still.

Share this post


Link to post
Share on other sites
the yellow 'search results bar' with the php error... thats where im putting the 'found emulators' code.

Share this post


Link to post
Share on other sites
Oh I see what you mean, my bad...

Change this:

[code]<?php
$database="database"; 
mysql_connect ("localhost", "user", "pass"); 
@mysql_select_db($database) or die( "Unable to select database");
$num_rows = mysql_num_rows($sql);
echo "<p class='footer'>"; 
echo $num_rows;
echo " emulators found</p>"; 
?>[/code]

To this:

[code]<?php
$database="database"; 
mysql_connect ("localhost", "user", "pass"); 
@mysql_select_db($database) or die( "Unable to select database");
$sql = "select emulator, version, os, platform, details from windows_atari2600 where emulator LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' OR platform LIKE '%".mysql_real_escape_string($_POST["emusearch"])."%' order by platform asc, emulator asc";
$result = mysql_query($sql);
$num_rows = mysql_num_rows($result);
echo "<p class='footer'>"; 
echo $num_rows;
echo " emulators found</p>"; 
?>[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
k, just done a bit of editting to that and its working fine! thanks a lot... not sure how this has actually worked... but its great!  Ill study it later on.

Share this post


Link to post
Share on other sites

×

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.