Jump to content

Recommended Posts

I have a search box inside the "inbox" area of my website, i want it so that when the user types in the username of the person that sent a message to them it should be able to count the number of results, and show them in a table of results.

 

However the problem im facing is that when you search for a message the result counter returns the correct value (say... 2), while the table only shows 1 result. I can't see or think for the life of me what is wrong with the code below...

 

<?php
include "inc/scriptstart.php";

$title = "Fusion Social | ";
$page = "Inbox";
$site = $title.$page;

session_start();
$u = $_SESSION["user"];
$dir = $_SESSION["dir"];

if(empty($u) || empty($dir)){
 header("Location: index.php");
}

$searchquery = $_GET["query"];

include "inc/check.php";
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "[url="http://www.w3.org/TR/html4/loose.dtd"]http://www.w3.org/TR/html4/loose.dtd[/url]">
<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
 <?php include "inc/scripts.php"; ?>
 <title><?=$site?></title>
</head>
<body>
 <div class="header">
  <div id="header">
<ul>
 <li><a href="<?=$GLOBALS["nav"]?>profile.php">Profile</a></li>
 <li><a href="<?//=$GLOBALS["nav"]?>#">Friends</a></li>
 <li><a href="<?=$GLOBALS["nav"]?>logout.php">Logout</a></li>
</ul>
<div id="search">
 <?php

  if($searcherr){
   echo '<div class="error">Error: '.$searcherr.'</div>';
  }

 ?>
 <form action="" method="POST">
  <table border="0">
   <tr>
	<td><input type="text" name="define" id="name" class="searchField" /></td>
	<td><input type="submit" name="search" value="Search" class="buttons" /></td>
   </tr>
  </table>
 </form>
</div>
<div class="clear"></div>
  </div>
 </div>
 <div class="content">
  <div class="main">
<div class="leftcol">
 <h2><?=$page?></h2>
 <img src="<?=$GLOBALS["nav"]?>images/vertical-rule.png" />
 <div class="clear"></div>
 <?php

  $results = select("*", "users, messages", "users.username = messages.to AND messages.to = '$u' AND messages.from LIKE '%$searchquery%'") or die(mysql_eror());
  $numsearch = mysql_num_rows($results);
  $getsearch = mysql_fetch_assoc($results);

  echo '<p>Results Found: '.$numsearch.'</p>';
  echo '<img src="'.$GLOBALS["nav"].'images/vertical-rule.png" />';
  echo '<div class="clear" style="height:30px;"></div>';

  echo '<table border="0">';
  echo '<th width="1%">Senders Avatar</th>';
  echo '<th width="25%">From</th>';
  echo '<th width="25%">To</th>';
  echo '<th width="25%">Options</th>';

  if($numsearch > 0){
   while($getfill = mysql_fetch_array($results)){
	echo '<tr>';
	 echo '<td width="1%">';
	  if(empty($getfill["avatar"])){
	   echo '<img src="'.$GLOBALS["nav"].'images/avatar.png" />';
	  }elseif(!empty($getfill["avatar"])){
	   echo '<img src="'.$getfill["avatar"].'" />';
	  }
	 echo '</td>';
	 echo '<td width="25%"><p>'.$getfill["from"].'</p></td>';
	 echo '<td width="25%"><p>'.$getfill["to"].'</p></td>';
	 echo '<td width="25%">';
	  echo '<a href="#">View Message</a>';
	  echo '<a href="#">Delete Message</a>';
	 echo '</td>';
	echo '</tr>';
   }
  }elseif($numsearch == 0){
   echo '<tr>';
	echo '<td colspan="4">';
	 echo "<p>There are currently no messages to view!</p>";
	echo '</td>';
   echo '</tr>';
  }

  echo '</table>';

 ?>
</div>
<img src="<?=$GLOBALS["nav"]?>images/home-divider.png" />
<!--<div class="rightcol">
 <h3></h3>
 <?php//

 ?>
 <img src="<?//=$GLOBALS["nav"]?>images/vertical-rule.png" />
</div>-->
<div class="clear"></div>
  </div>
 </div>
 <div class="footer">
  <p>Copyright © <?=date("Y")?> Fusion Social</p>
 </div>
</body>
</html>

 

Any Ideas?

Link to comment
https://forums.phpfreaks.com/topic/270316-searchng-for-particular-messages/
Share on other sites

$getsearch = mysql_fetch_assoc($results);

 

^^^ Why do you have a line of code that is fetching a row from the result set before the start of your while(){} loop? Wouldn't that mean that your while(){} loop will start at the second record in the result set?

$results = select("*", "users, messages", "users.username = messages.to AND messages.to = '$u' AND messages.from LIKE '%$searchquery%'") or die(mysql_eror());

 

You misspelled mysql_error there may be a problem with your query. (Doubtful)

Edited by ExtremeGaming

If you removed that line of code and there was no change in the output, then either you didn't save the file, upload the file to your server, or caching somewhere between the file and your browser is showing the previous results.

 

If you are getting a mysql_num_rows value of 2, then running a total of 2 mysql_fetch_assoc statements will fetch those 2 rows.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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