Jump to content

Recommended Posts

I want to be able to select all user if All Users

are selected from the drop down menu.

 

Example: Click Here!!

 

$searchtype = ($_POST['searchtype']);
$all_users = ($_POST['all_users']);

$query="SELECT * FROM people where sponsor='$searchtype'";

echo "<table border=1 width=50% align=center>
<tr>
<td align=center>Name</td>
<td align=center>Age</td>
<td align=center>State</td>
<td align=center>Sponsor</td>
</tr>";

$result = mysql_query($query);
$num_results = mysql_num_rows($result); 
    
for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($result);
  
$name = htmlspecialchars( stripslashes($row["name"]));
$age = htmlspecialchars( stripslashes($row["age"]));
$state = htmlspecialchars( stripslashes($row["state"]));
$sponsor = htmlspecialchars( stripslashes($row["sponsor"]));

// Output
echo " <tr>
<td>$name</td>
<td>$age</td>
<td>$state</td>
<td>$sponsor</td>";

}// End of For loop
   
echo "</tr>
</table>";

if($searchtype==$all_users){
  $query2 = "SELECT * FROM people";
}

$query2 = "SELECT DISTINCT sponsor FROM people";
$result2 = mysql_query($query2);
$num_results2 = mysql_num_rows($result2);

echo "<form name=\"auto\" method=post>";
echo "<select name=\"searchtype\" onChange=\"auto.submit();\">
<option SELECTED>Sponsor</option>";
echo "<option name=\"all_users\">All Users</option>";
for ($i = 0; $i < $num_results2; $i++) {
    $row = mysql_fetch_array($result2);
    echo "<option value=\"$row[sponsor]\">$row[sponsor]</option>\n";
} 
echo "</select>";
//echo "<input type=\"submit\" value=\"Submit\" />";
echo "</form>";

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

try

$searchtype = ($_POST['searchtype']);
$all_users = ($_POST['all_users']);
if($all_useers='all_users') $x = '1'; else $x="sponsor='$searchtype'";
$query="SELECT * FROM people where ".$x;
etc.

 

I couldn't get it working. Here is what I tried.

 

if($all_user=$searchtype){ $query="SELECT * FROM people";}

else{echo "Could not run query";}

I was wondering about wether or not I was

on the right path with this. I am trying to populate

a table wiht every person in the database if $all_users

is selected in the drop down menu. Please read comments

in code where it says //COMMENTS?

 

Example; Click Here!!

 

$searchtype = ($_POST['searchtype']);	
$all_users = ($_POST['all_users']);

//COMMENTS
/* Ok, if $searchtype == something
then select all from database. $query="SELECT * FROM people";
is not returning true because it is skipped. Why*/

if($searchtype == "$all_users"){
     $query="SELECT * FROM people";
}else{
$query="SELECT * FROM people where sponsor='$searchtype'";}

echo "<table border=1 width=50% align=center>
<tr>
<td >Name</td>
<td >Age</td>
<td >State</td>
<td >Sponsor</td>
</tr>";

$result = mysql_query($query);
$num_results = mysql_num_rows($result); 
    
for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($result);
  
$name = htmlspecialchars( stripslashes($row["name"]));
$age = htmlspecialchars( stripslashes($row["age"]));
$state = htmlspecialchars( stripslashes($row["state"]));
$sponsor = htmlspecialchars( stripslashes($row["sponsor"]));

// Output
echo " <tr>
<td>$name</td>
<td>$age</td>
<td>$state</td>
<td>$sponsor</td>";

}// End of For loop
   
echo "</tr>
</table>";

$query2 = "SELECT DISTINCT sponsor FROM people";
$result2 = mysql_query($query2);
$num_results2 = mysql_num_rows($result2);

echo "<form name=\"auto\" method=post>";
echo "<select name=\"searchtype\" onChange=\"auto.submit();\">
<option SELECTED>Sponsor</option>";

 

Rest of code!!

//COMMENTS
/* I need a value from this line
so why isn't it returning a value?

echo "<option name=\"all_users\" >All Users</option>";

for ($i = 0; $i < $num_results2; $i++) {
$row = mysql_fetch_array($result2);
echo "<option value=\"$row[sponsor]\">$row[sponsor]</option>\n";
} 
echo "</select>";
//echo "<input type=\"submit\" value=\"Submit\" />";
echo "</form>";

 

I was wondering about wether or not I was

on the right path with this. I am trying to populate

a table wiht every person in the database if $all_users

is selected in the drop down menu. Please read comments

in code where it says //COMMENTS?

 

Example; Click Here!!

 

<?php
$searchtype = ($_POST['searchtype']);	
$all_users = ($_POST['all_users']);

//COMMENTS
/* Ok, if $searchtype == something
then select all from database. $query="SELECT * FROM people";
is not returning true because it is skipped. Why*/

if($searchtype == "$all_users"){
     $query="SELECT * FROM people";
}else{
$query="SELECT * FROM people where sponsor='$searchtype'";}

echo "<table border=1 width=50% align=center>
<tr>
<td >Name</td>
<td >Age</td>
<td >State</td>
<td >Sponsor</td>
</tr>";

$result = mysql_query($query);
$num_results = mysql_num_rows($result); 
    
for ($i = 0; $i < $num_results; $i++) {
$row = mysql_fetch_array($result);
  
$name = htmlspecialchars( stripslashes($row["name"]));
$age = htmlspecialchars( stripslashes($row["age"]));
$state = htmlspecialchars( stripslashes($row["state"]));
$sponsor = htmlspecialchars( stripslashes($row["sponsor"]));

// Output
echo " <tr>
<td>$name</td>
<td>$age</td>
<td>$state</td>
<td>$sponsor</td>";

}// End of For loop
   
echo "</tr>
</table>";

$query2 = "SELECT DISTINCT sponsor FROM people";
$result2 = mysql_query($query2);
$num_results2 = mysql_num_rows($result2);

echo "<form name=\"auto\" method=post>";
echo "<select name=\"searchtype\" onChange=\"auto.submit();\">
<option SELECTED>Sponsor</option>";
?>

 

Rest of code!!

//COMMENTS
/* I need a value from this line
so why isn't it returning a value?

echo "<option name=\"all_users\" >All Users</option>";

for ($i = 0; $i < $num_results2; $i++) {
$row = mysql_fetch_array($result2);
echo "<option value=\"$row[sponsor]\">$row[sponsor]</option>\n";
} 
echo "</select>";
//echo "<input type=\"submit\" value=\"Submit\" />";
echo "</form>";

 

/* I need a value from this line

so why isn't it returning a value?

 

echo "<option name=\"all_users\" >All Users</option>";

 

You haven't specified a value, you specified a name

 

Change it to

 

echo "<option value='all_users' >All Users</option>";

Would I use isset($searchtype) because I don't

want it to automatically populate the table.

 

Example: Click Here!!

if($searchtype == "$all_users"){
	$query="SELECT * FROM people";
}else{
$query="SELECT * FROM people where sponsor='$searchtype'";}


             echo "<option value=\"$all_users\" >All Users</option>";

isset($searchtype) will always return "true" no matter what value it has, so you need to check its value as you have in your code ^

 

Ok, even when I take out $all_users it's populating

the table still. That means that if($searchtype == $all_users)

is returning true still, why?

 

echo "<option value=\"\" >All Users</option>";

echo "<option value=\"$all_users\" >All Users</option>";

You want:

<?php
echo '<option value="all_users" >All Users</option>';
?>

Then the "if" statement

<?php
if ($searchtype == 'all_users')
?>

would work.

 

Ken

 

Ken it worked but what is the differents between the two? Thanks!

echo '<option value="all_users" >All Users</option>';

echo "<option value="all_users" >All Users</option>";

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.