Page 1 of 1

[SOLVED]Search Bar Issue.

Posted: Sun Aug 07, 2011 5:17 am
by ThatPerson
I am trying to make a user search bar for a sort of social network type thing so you can search for friends, and it returns their name, email, description, and buttons saying [text]'Send friend request'[/text] or [text]'View Profile'[/text]

I have been working on it for quite a while but cannot see why it returns nothing (eg I have two users, complete with names and emails and descriptions, called Jeremy and Bethany (Test accounts, not anything linking to me), and it returns:

[text]Search: Jer[/text]
When it should return:
[text]Search: Jer, Name = Jeremy, Email = Jeremy@fakeemailaddress.co.uk, Description = Just on here for a while, Add as friend[/text]

It also does not work even with the full address:

Code: Select all

<?php
ob_start();
include("database.php");
echo <<<END
<head>
<title>Blackboard</title>
</head>
END;
echo <<<END
<div>
<FORM NAME ="form1" METHOD ="POST" ACTION = "search.php">
<INPUT TYPE = "Text" VALUE ="" NAME = "search">

<INPUT TYPE = "Submit" Name = "Search1" VALUE = "Search">
</div>
END;
if (isset($_POST['Search1'])) {

$term = $_POST['search'];

$term2 = $term.'%';

$search = mysql_query('SELECT email FROM notes_notes WHERE Username LIKE "$term2"') or die(mysql_error());
$user = $_COOKIE['loggeduser'];
$search2 = mysql_fetch_array($search) or die(mysql_error());
$search8 = $search2 -> Username;
echo $search8;
$friends = mysql_query("select friends from notes_notes where email like '$user'");
$friends2 = mysql_fetch_object($friends);
$friends3 = $friends2->friends;
$friendreq = mysql_query("select friendrequests from notes_notes where email like '$user'");
$friendreq2 = mysql_fetch_object($friendreq);
$friendreq4 = $friendreq2->friendrequests;


echo $term;
$p = 0;
foreach ($search2 as &$value) {
$p=$p+1;
echo $value;
$search4 = $value->Username;
$desc = mysql_query("select description from notes_notes where email like '$value'");
echo $value;
$desc2 = mysql_fetch_object($desc);
$desc3 = $desc->description;
${n.$p} = $value;
if (strpos($friends3,$value) == false) {
echo <<<END
    <div style="height:30px; width:300px;">
    $value
    <br>
    $desc3
    <input type="Submit" value="Send friend request" name = "friend$p">
    </div>
END;
}
else
{
echo <<<END
    <div style="height:30px; width:300px;">
    $value
    <br>
    $desc3
    <input type="Submit" value="View Profile" name = "only$p">
    </div>
END;
}
}
unset($value);

} else { 
}

$a = 0;
for ($i = 1; $i <= $len; $i++) {
 $a = $a + 1;
 $name = "only".$a;

if (isset($_POST[$name])) {

  $name = ${n.$a};
  setcookie("loggedfriend",$_COOKIE['loggeduser'],time()+60*60*24);
  echo <<<end
<head>
<meta HTTP-EQUIV="REFRESH" content="0; url='guestnotes.php'">
</head>
end;
   
   
   
   
  }
}

$z = 0;
for ($i = 1; $i <= $len; $i++) {
 $z = $z + 1;
 $namen = "friend".$z;

if (isset($_POST[$namen])) {

  $name = ${n.$a};
  $player = $_COOKIE['loggeduser'];
   $oldfriend = mysql_query("select friendrequests from notes_notes where email = '$name'") or die(mysql_error());
  $oldfriend2 = mysql_fetch_object($oldfriend);
  $oldfriend3 = $oldfriend2 -> friendrequests;
 
   $old = $oldfriends3.$player;

   mysql_query("UPDATE notes_notes SET friendrequests = '$old' WHERE email = '$name}'") or die(mysql_error());
   
   
  }
}

?>
Also, I am trying to condense all of my PHP code onto one website instead of seperated, if you can see anyways I could make the length of code shorter, please do tell :D

Re: Search Bar Issue.

Posted: Sun Aug 07, 2011 5:56 am
by ThatPerson
Right, in my quest to condense it, I have rewritten the code, so if you find any reasons it would not work, please tell. Also, on this code I get a
[text]Warning: Invalid argument supplied for foreach() on line 20[/text]
And line 20 is pointed out in the code if you can see the reason:

Code: Select all

<?php
ob_start();
include("database.php");
echo <<<END
<head>
<title>Blackboard</title>
</head>
<div>
<FORM NAME ="form1" METHOD ="POST" ACTION = "search.php">
<INPUT TYPE = "Text" VALUE ="" NAME = "search">
<INPUT TYPE = "Submit" Name = "Search1" VALUE = "Search">
</div>
END;
if (isset($_POST['Search1'])) {
$term = $_POST['search'].'%';
$search = mysql_fetch_array(mysql_query('SELECT email FROM notes_notes WHERE Username LIKE "$term"')); #It calls on this line, which should return a array.
$user = $_COOKIE['loggeduser'];
$friends = mysql_fetch_object(mysql_query("select friends from notes_notes where email like '$user'")) ->friends;
$p = 0;
foreach ($search as &$value) { #This is line 20, which is getting an error
$p=$p+1;
$search4 = $value->Username;
$desc = mysql_fetch_object(mysql_query("select description from notes_notes where email like '$value'"))->description;
${n.$p} = $value;
echo <<<END
    <div style="height:30px; width:300px;">
    <h2>$value</h2>
    <br>
    <p>$desc</p>
END;
    if (strpos($friends,$value) == false) {
     echo '<input type="Submit" value="Send friend request" name = "friend$p">';
    }
    else {
     echo '<input type="Submit" value="View Profile" name = "only$p">';
    }
echo <<<END
    </div>
END;
}
unset($value);
} else { 
}
$a = 0;
for ($i = 1; $i <= $len; $i++) {
 $a = $a + 1;
 $sendFriendRequest = "friend".$a;
 $viewProfile = "only".$a;
 $playerName = ${n.$a};
 if (isset($_POST[$viewProfile])) {
  setcookie("loggedfriend",$playerName,time()+60*60*24);
echo <<<end
<head>
<meta HTTP-EQUIV="REFRESH" content="0; url='guestnotes.php'">
</head>
end;
 }
 if (isset($_POST[$namen])) {
  $player = $_COOKIE['loggeduser'];
  $oldFriend = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$name'") or die(mysql_error()))->friendrequests;
  $newFriends = $oldFriends.$playerName;
  mysql_query("UPDATE notes_notes SET friendrequests = '$newFriends' WHERE email = '$playerName'") or die(mysql_error());
 }
}
?>

Re: Search Bar Issue.

Posted: Sun Aug 07, 2011 7:43 am
by oscardog
Few things to try. Firstly, after you set $search put var_dump($search); and see what is displayed. It should output the array on the screen, plus in the foreach (as it's an associative array) you should use foreach($search as $key => $value).

Whenever you use strpos() you should always use === not == as described in the PHP docs.

Re: Search Bar Issue.

Posted: Sun Aug 07, 2011 10:38 am
by ThatPerson
Such as:

Code: Select all

<?php
ob_start();
include("database.php");
echo <<<END
<head>
<title>Blackboard</title>
</head>
<div>
<FORM NAME ="form1" METHOD ="POST" ACTION = "search.php">
<INPUT TYPE = "Text" VALUE ="" NAME = "search">
<INPUT TYPE = "Submit" Name = "Search1" VALUE = "Search">
</div>
END;
if (isset($_POST['Search1'])) {
$term = $_POST['search'].'%';
$search = mysql_fetch_array(mysql_query('SELECT email FROM notes_notes WHERE Username LIKE "$term"'));
$user = $_COOKIE['loggeduser'];
$friends = mysql_fetch_object(mysql_query("select friends from notes_notes where email like '$user'")) ->friends;
$p = 0;
foreach ($search as &$value=>Username) {
$p=$p+1;
$search4 = $value->Username;
$desc = mysql_fetch_object(mysql_query("select description from notes_notes where email like '$value'"))->description;
${n.$p} = $value;
echo <<<END
    <div style="height:30px; width:300px;">
    <h2>$value</h2>
    <br>
    <p>$desc</p>
END;
    if (strpos($friends,$value) === false) {
     echo '<input type="Submit" value="Send friend request" name = "friend$p">';
    }
    else {
     echo '<input type="Submit" value="View Profile" name = "only$p">';
    }
echo <<<END
    </div>
END;
}
unset($value);
} else { 
}
$a = 0;
for ($i = 1; $i <= $len; $i++) {
 $a = $a + 1;
 $sendFriendRequest = "friend".$a;
 $viewProfile = "only".$a;
 $playerName = ${n.$a};
 if (isset($_POST[$viewProfile])) {
  setcookie("loggedfriend",$playerName,time()+60*60*24);
echo <<<end
<head>
<meta HTTP-EQUIV="REFRESH" content="0; url='guestnotes.php'">
</head>
end;
 }
 if (isset($_POST[$namen])) {
  $player = $_COOKIE['loggeduser'];
  $oldFriend = mysql_fetch_object(mysql_query("select friendrequests from notes_notes where email = '$name'") or die(mysql_error()))->friendrequests;
  $newFriends = $oldFriends.$playerName;
  mysql_query("UPDATE notes_notes SET friendrequests = '$newFriends' WHERE email = '$playerName'") or die(mysql_error());
 }
}
?>

Re: Search Bar Issue.

Posted: Sun Aug 07, 2011 10:43 am
by ThatPerson
oscardog wrote:Few things to try. Firstly, after you set $search put var_dump($search); and see what is displayed. It should output the array on the screen, plus in the foreach (as it's an associative array) you should use foreach($search as $key => $value).

Whenever you use strpos() you should always use === not == as described in the PHP docs.
I did the var_dump and it came out with bool(false), which is most likely the error. I am going to convert it to a string and edit this post with the answer.

Edit: I replaced it with:

Code: Select all

$search = mysql_fetch_object(mysql_query(select email from notes_notes where Username like '$term')) -> email
Yet now it returns NULL. Probably because it has no information, yet the name is in the database. I could try fetching the Username?

Edit: Yet it still returns NULL. Oh, and the 'Warning: Invalid argument supplied for foreach() in notes/search.php on line 22' error still arises.

Re: Search Bar Issue.

Posted: Sun Aug 07, 2011 10:53 am
by ThatPerson
Yay, I modified the mysql_fetch_object (May have been array) to mysql_fetch_array (Possibly object) and now it works :D. Thanks for the help!