Jump to content

Recommended Posts

Hi guys, I have a problem and I am well and truley bamboozled. I have a table which displays results from a search and then displays a link underneath in each result. However I only want this link to show if the results match those in the database. In this case I have a userid (created in session when logged in) and if it is in either column one or column two in my database.

 

My problem comes that it either shows all records with a link, or all without. I hope that makes sense and cheers for helping.

 

Nick

 

		<?php
	session_start();
	require "../Connect.php" ;
	$sessionid = $_SESSION['pid'];

	if($_GET['people']== "")
	{
		header("Location:../my/no-records.php?");
		exit();
	}

	else
	{
		$query = "SELECT * FROM member WHERE fname like '%".$_GET['people']."%' or sname like '%".$_GET['people']."%' ORDER BY sname";



	}

	$result = mysql_query($query, $connection) or die ("unable to perform query <br/> $query");?>

	<?php
	$query3 = "select * from userfriends where pid= '".$sessionid."' or friend='".$sessionid."'";
	$result3 = mysql_query($query3, $connection) or die ("unable to perform query <br/> $query3");
	$row3 = mysql_fetch_array($result3);
	?>





	<!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">

<!-- Holiday-Friends.Com -->
<!-- Built by Business Information Technology Students -->
<!-- Representing Bournemouth University England -->
<!-- All website built and owned by Adam Dale, Nick Whitmarsh and Daniel Salter -->

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Holiday-Friends.Com</title>
<style type="text/css" media="all">

	@import "../css/loggedinsheet.css";

</style>
</head>

<body>

<div id="page">
<div id="container"> 
  <div id="top">
  
  <div id="logo"><a href="../search/index.html"><img src="../logo.PNG" width="298" height="35" border="0" /></a></div>
  
    <div id="top-l"><img src="../f4_1.GIF" /></div>
<div id="top-r"><img src="../f4_2.GIF" /></div>

  <?php include ("s-box.html") ?>
  </div>
  <div id="sidemenu">
    <div class="menutitle">Menu:</div>
<?php include ("menu.html") ?>
  </div>
  <div id="flash-holder"> flash animation</div>
  <div id="interaction">
<?php include ("box.html") ?>
  </div>
  <div id="advert">advert</div>
  <div id="advert-2">advert 2</div>
  <div id="content">
  
  			<table>
	<tr>
	</tr>
	<?php
	while($row= mysql_fetch_array($result))
	{
		$searchpid = $row['pid'];
		$query1 = "SELECT * FROM profile where pid = '".$searchpid."'";
		$result1 = mysql_query($query1, $connection) or die ("unable to perform query <br/> $query1");
		$row1= mysql_fetch_array($result1);


	if ($row1['picture'] == "")
	{
		$FILENAME = "imageunavailable.png";
	}
	else
	{
		$FILENAME =  $row1['picture'];
	}
	?>
		<tr>
		<td><img src= "../my/pictures/<?php echo ($FILENAME)?>" width = "100" height = "75" border="0"></td>
		<td><b>Name:</b><?php echo" "; echo($row['fname']); echo" "; echo($row['sname']); ?>
		<br><b>Next Hol:</b><?php echo" "; echo($row['email'])?>




		<?php
		$friend = $row3['friend'];
		$you = $row3['pid'];

		if($friend != $sessionid or $you != $sessionid)
		{
		?>
		<br><a href="fr.php?PID=<?php echo $searchpid ?>">Add to friends</a>
		<?php
		}
		else
		{

		}?>

		</td>
		</tr>
	<?php
	}
	?>
	</table>
  

  </div>
</div>
</div>

<?php include ("footer.html") ?>
</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/54344-if-statement-help/
Share on other sites

This makes no sense to me "I only want this link to show if the results match those in the database.". Aren't the results the records that came from the database? I think you mean you only want to show the link if the session id matches the "friend" or "you" ids from the results, correct?

 

The problem is your IF condition

 

if($friend != $sessionid or $you != $sessionid)

 

You are using two negatives with an OR. Unless $friend and $you are the same ID the statment will always be false because if $sessionid equals $friend, then it doesn't equal $you.

 

Isn't this what you want?

 

if($friend == $sessionid or $you == $sessionid)

 

Also, your code is kind of sloppy with some improper HTML being produced. I think this is a little cleaner:

<?php

session_start();
require "../Connect.php" ;
$sessionid = $_SESSION['pid'];

if($_GET['people']== "")
{
header("Location:../my/no-records.php?");
exit();
}

else
{
$query = "SELECT * FROM member WHERE fname like '%".$_GET['people']."%' or sname like '%".$_GET['people']."%' ORDER BY sname";
}

$result = mysql_query($query, $connection) or die ("unable to perform query <br/> $query");

$query3 = "select * from userfriends where pid= '".$sessionid."' or friend='".$sessionid."'";
$result3 = mysql_query($query3, $connection) or die ("unable to perform query <br/> $query3");
$row3 = mysql_fetch_array($result3);
?>

<!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">

<!-- Holiday-Friends.Com -->
<!-- Built by Business Information Technology Students -->
<!-- Representing Bournemouth University England -->
<!-- All website built and owned by Adam Dale, Nick Whitmarsh and Daniel Salter -->

<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Holiday-Friends.Com</title>

<style type="text/css" media="all">
    @import "../css/loggedinsheet.css";
</style>

</head>

<body>

<div id="page">
<div id="container"> 
  <div id="top">
    <div id="logo"><a href="../search/index.html"><img src="../logo.PNG" width="298" height="35" border="0" /></a></div>
    <div id="top-l"><img src="../f4_1.GIF" /></div>
    <div id="top-r"><img src="../f4_2.GIF" /></div>
    <?php include ("s-box.html") ?>
  </div>
  <div id="sidemenu">
    <div class="menutitle">Menu:</div>
    <?php include ("menu.html") ?>
  </div>
  <div id="flash-holder"> flash animation</div>
  <div id="interaction">
    <?php include ("box.html") ?>
  </div>
  <div id="advert">advert</div>
  <div id="advert-2">advert 2</div>
  <div id="content">
  
<?php

echo "<table>";

while($row= mysql_fetch_array($result))
{
    $searchpid = $row['pid'];
    $query1 = "SELECT * FROM profile where pid = '".$searchpid."'";
    $result1 = mysql_query($query1, $connection) or die ("unable to perform query <br/> $query1");
    $row1= mysql_fetch_array($result1);

    $FILENAME = ($row1['picture'] == "") ? "imageunavailable.png" : $row1['picture'] ;
    $friend = $row3['friend'];
    $you = $row3['pid'];

    $link = "";
    if ($friend == $sessionid or $you == $sessionid)
    {
        $link = "<br><a href=\"fr.php?PID=$searchpid\">Add to friends</a>";
    }

    echo "<tr>\n";
    echo "<td><img src=\"../my/pictures/$FILENAME\" width=\"100\" height=\"75\" border=\"0\"></td>\n";
    echo "<td><b>Name:</b> $row['fname'] $row['sname']<br>\n";
    echo "  <b>Next Hol:</b> $row['email']" . $link;
    echo "</td></tr>\n";
}

echo "</table>";

?>

  </div>
</div>

<?php include ("footer.html") ?>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/54344-if-statement-help/#findComment-268766
Share on other sites

Hi Nick

 

Not entirely sure if this is what you are trying to acheive, but I hope it helps:

 

<?php
session_start();
include_once('../Connect.php') ;
$sessionid = $_SESSION['pid'];

if($_GET['people']== ''){
    header('Location:../my/no-records.php?');
} else {
    $query  =  'SELECT 
                    * 
                FROM 
                    member 
                WHERE 
                    fname LIKE "%' . $_GET['people'] . '%" OR
                    sname LIKE "%' . $_GET['people'] . '%" 
                ORDER BY 
                    sname;
               ';
}
$result     = mysql_query($query, $connection) or die ("unable to perform query <br/> $query");
$query3     =  'SELECT 
                    * 
                FROM 
                    userfriends 
                WHERE 
                    pid     = "' . $sessionid . '" OR 
                    friend  = "' . $sessionid . '";
               ';
$result3    = mysql_query($query3, $connection) or die ("unable to perform query <br/> $query3");
$row3       = mysql_fetch_assoc($result3);
?>
<!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">
<!-- Holiday-Friends.Com -->
<!-- Built by Business Information Technology Students -->
<!-- Representing Bournemouth University England -->
<!-- All website built and owned by Adam Dale, Nick Whitmarsh and Daniel Salter -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Holiday-Friends.Com</title>
<style type="text/css" media="all">
    @import "../css/loggedinsheet.css";
</style>
</head>
<body>
<div id="page">
    <div id="container"> 
        <div id="top">
            <div id="logo"><a href="../search/index.html"><img src="../logo.PNG" width="298" height="35" border="0" /></a></div>
            <div id="top-l"><img src="../f4_1.GIF" /></div>
            <div id="top-r"><img src="../f4_2.GIF" /></div>
            <?php include ("s-box.html") ?>
        </div>
        <div id="sidemenu">
            <div class="menutitle">Menu:</div>
            <?php include ("menu.html") ?>
        </div>
        <div id="flash-holder">flash animation</div>
        <div id="interaction">
            <?php include ("box.html") ?>
        </div>
        <div id="advert">advert</div>
        <div id="advert-2">advert 2</div>
        <div id="content">
            <table>
                <?php
                while($row = mysql_fetch_assoc($result)){
                    $searchpid  = $row['pid'];
                    $query1     =  "SELECT 
                                        * 
                                    FROM 
                                        profile 
                                    WHERE 
                                        pid = '" . $searchpid . "';
                                   ";
                    $result1    = mysql_query($query1, $connection) or die ("unable to perform query <br/> $query1");
                    $row1       = mysql_fetch_assoc($result1);

                    if ($row1['picture'] == ""){
                        $filename = "imageunavailable.png";
                    } else {
                        $filename = $row1['picture'];
                    }
                    echo '
                    <tr>
                        <td><img src= "../my/pictures/' . $filename . '" width="100" height="75" border="0"></td>
                        <td>
                            <b>Name:</b> ' . $row['fname'] . ' ' . $row['sname'] . '
                            <br>
                            <b>Next Hol:</b> ' . $row['email'];                           
                            //$friend = $row3['friend'];
                            //$you    = $row3['pid'];
                            foreach($row3 as $key=>$value){
                                if($value != $searchpid){
                                    echo '<br><a href="fr.php?PID=' . $searchpid . '">Add to friends</a>';
                                }
                            }
                    echo '
                    </td>
                </tr>';
                }
                ?>
            </table>
        </div>
    </div>
</div>
<?php include_once('footer.html') ?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/54344-if-statement-help/#findComment-268799
Share on other sites

Hi Nick

 

Replace this:

 

foreach($row3 as $key=>$value){
    if($value != $searchpid){
        echo '<br><a href="fr.php?PID=' . $searchpid . '">Add to friends</a>';
        break;
    }
}

 

with this:

 

$link = true;
foreach($row3 as $key=>$value){
    if($value == $searchpid){
        $link = false;
    }
}
if($link){
    echo '<br><a href="fr.php?PID=' . $searchpid . '">Add to friends</a>';
}

Link to comment
https://forums.phpfreaks.com/topic/54344-if-statement-help/#findComment-269089
Share on other sites

Ok I have fixed it so that it does not show the link multilpe times, however say my database result looks like this :

 

pid    friend

1        2

2        1

1        5

5        1

 

It does not show the links for the first two results, which is correct, however it does show them for the second two, which it shouldnt do.

 

Here i my code as it looks

 

Cheers

<?php
session_start();
include_once('../Connect.php') ;
$sessionid = $_SESSION['pid'];

if($_GET['people']== ''){
   header('Location:../my/no-records.php?');
} else {
   $query  =  'SELECT 
                   * 
               FROM 
                   member 
               WHERE 
                   fname LIKE "%' . $_GET['people'] . '%" OR
                   sname LIKE "%' . $_GET['people'] . '%" 
               ORDER BY 
                   sname;
              ';
}
$result     = mysql_query($query, $connection) or die ("unable to perform query <br/> $query");

?>
<!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">
<!-- Holiday-Friends.Com -->
<!-- Built by Business Information Technology Students -->
<!-- Representing Bournemouth University England -->
<!-- All website built and owned by Adam Dale, Nick Whitmarsh and Daniel Salter -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Holiday-Friends.Com</title>
<style type="text/css" media="all">
   @import "../css/loggedinsheet.css";
</style>
</head>
<body>
<div id="page">
   <div id="container"> 
       <div id="top">
           <div id="logo"><a href="../search/index.html"><img src="../logo.PNG" width="298" height="35" border="0" /></a></div>
           <div id="top-l"><img src="../f4_1.GIF" /></div>
           <div id="top-r"><img src="../f4_2.GIF" /></div>
           <?php include ("s-box.html") ?>
       </div>
       <div id="sidemenu">
           <div class="menutitle">Menu:</div>
           <?php include ("menu.html") ?>
       </div>
       <div id="flash-holder">flash animation</div>
       <div id="interaction">
           <?php include ("box.html") ?>
       </div>
       <div id="advert">advert</div>
       <div id="advert-2">advert 2</div>
       <div id="content">
           <table>
               <?php
               while($row = mysql_fetch_assoc($result)){
                   $searchpid  = $row['pid'];
                   $query1     =  "SELECT 
                                       * 
                                   FROM 
                                       profile 
                                   WHERE 
                                       pid = '" . $searchpid . "';
                                  ";
                   $result1    = mysql_query($query1, $connection) or die ("unable to perform query <br/> $query1");
                   $row1       = mysql_fetch_assoc($result1);

                   if ($row1['picture'] == ""){
                       $filename = "imageunavailable.png";
                   } else {
                       $filename = $row1['picture'];
                   }
                   echo '
                   <tr>
                       <td><img src= "../my/pictures/' . $filename . '" width="100" height="75" border="0"></td>
                       <td>
                           <b>Name:</b> ' . $row['fname'] . ' ' . $row['sname'] . '
                           <br>
                           <b>Next Hol:</b> ' . $row['email'];                           
                          
					   
					   
 $query3     =  'SELECT 
                   * 
               FROM 
                   userfriends 
               WHERE 
                   pid     = "' . $sessionid . '" OR 
                   friend  = "' . $sessionid . '";
              ';


		$result3    = mysql_query($query3, $connection) or die ("unable to perform query <br/> $query3");
		$row3       = mysql_fetch_array($result3);
					  
					    $friend = $row3['friend'];
                           $you    = $row3['pid'];


                               if($sessionid != $searchpid and $friend != $searchpid){

                                   echo '<br><a href="fr.php?PID=' . $searchpid . '">Add to friends</a>';
                               }



                   echo '
                   </td>
               </tr>';
               }
               ?>
           </table>
       </div>
   </div>
</div>
<?php include_once('footer.html') ?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/54344-if-statement-help/#findComment-269090
Share on other sites

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.