Trium918 Posted April 3, 2007 Share Posted April 3, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/ Share on other sites More sharing options...
Trium918 Posted April 3, 2007 Author Share Posted April 3, 2007 I'm sorry for those who have viewed this topic for not having the link working. It is now up and running. Example: Click Here: Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-220753 Share on other sites More sharing options...
papaface Posted April 3, 2007 Share Posted April 3, 2007 You don't need this: $all_users = ($_POST['all_users']); and change: if($searchtype==$all_users){ to: if($searchtype== "all_users"){ See what you get. Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-220755 Share on other sites More sharing options...
Trium918 Posted April 3, 2007 Author Share Posted April 3, 2007 You don't need this: $all_users = ($_POST['all_users']); and change: if($searchtype==$all_users){ to: if($searchtype== "all_users"){ See what you get. Table didn't populate. Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-220758 Share on other sites More sharing options...
Trium918 Posted April 3, 2007 Author Share Posted April 3, 2007 Any Ideas Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-220768 Share on other sites More sharing options...
sasa Posted April 3, 2007 Share Posted April 3, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-220770 Share on other sites More sharing options...
Trium918 Posted April 3, 2007 Author Share Posted April 3, 2007 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";} Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-220789 Share on other sites More sharing options...
Trium918 Posted April 3, 2007 Author Share Posted April 3, 2007 Can someone please help me out? Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-220836 Share on other sites More sharing options...
trq Posted April 3, 2007 Share Posted April 3, 2007 Can someone please help me out? You have failed to ask an actual question. Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-220838 Share on other sites More sharing options...
Trium918 Posted April 4, 2007 Author Share Posted April 4, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221359 Share on other sites More sharing options...
trq Posted April 4, 2007 Share Posted April 4, 2007 Looks good to me. Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221362 Share on other sites More sharing options...
Trium918 Posted April 4, 2007 Author Share Posted April 4, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221369 Share on other sites More sharing options...
Trium918 Posted April 4, 2007 Author Share Posted April 4, 2007 Does anyone have any Ideas? Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221383 Share on other sites More sharing options...
freakus_maximus Posted April 4, 2007 Share Posted April 4, 2007 if($searchtype == "$all_users"){ On this line, your IF is never going to be true. Remove the quotes you have around $all_users. Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221431 Share on other sites More sharing options...
Barand Posted April 4, 2007 Share Posted April 4, 2007 /* 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221456 Share on other sites More sharing options...
Trium918 Posted April 4, 2007 Author Share Posted April 4, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221520 Share on other sites More sharing options...
Barand Posted April 4, 2007 Share Posted April 4, 2007 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 ^ Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221530 Share on other sites More sharing options...
Trium918 Posted April 4, 2007 Author Share Posted April 4, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221539 Share on other sites More sharing options...
kenrbnsn Posted April 4, 2007 Share Posted April 4, 2007 You want: <?php echo '<option value="all_users" >All Users</option>'; ?> Then the "if" statement <?php if ($searchtype == 'all_users') ?> would work. Ken Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221541 Share on other sites More sharing options...
Trium918 Posted April 4, 2007 Author Share Posted April 4, 2007 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>"; Quote Link to comment https://forums.phpfreaks.com/topic/45465-solved-help-with-if-statement/#findComment-221552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.