kevincro Posted October 28, 2007 Share Posted October 28, 2007 I'm using an if construct to see if a members name has been added to a list of applicants. If they have been, my code is set to dispay an unapply button. If they have not yet applied(added their names to the database) an apply button is displayed. The code, in its present form, looks as follows: <? $query=mysql_query("SELECT Name FROM Applicants WHERE Number2='$data_key'"); $row=mysql_fetch_array($query); if ($userdata['user_name']!= $row['Name']) { print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"apply\" VALUE=\"Apply\">"."</td>"."</tr>\n"; } else { print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"unapply\" VALUE=\"Unapply\">"."</td>"."</tr>\n"; } ?> The expression for my if construct works great, but only if noone has applied, or the member is the first name on the list. So my question is, is there an expression that will search through the column data and if the members name is already on the list, display the unapply button? $data_key is given the value of the project number, $userdata['user_name'] is a global that is constantly set to the members user name. Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 28, 2007 Share Posted October 28, 2007 Try: <?php $query = mysql_query("SELECT COUNT(*) FROM `Applicants` WHERE `Name`='".$userdata['user_name']."'") or die(mysql_error(); $num = mysql_result($query,0); if($num == 0){ print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"apply\" VALUE=\"Apply\">"."</td>"."</tr>\n"; }else{ print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"unapply\" VALUE=\"Unapply\">"."</td>"."</tr>\n"; } ?> The idea is that you find out how many rows exist with that given name. If it is 0, then you know it's not in the database. Quote Link to comment Share on other sites More sharing options...
spfoonnewb Posted October 28, 2007 Share Posted October 28, 2007 <?php $query = mysql_query("SELECT `Name` FROM `Applicants` WHERE `Number2` = '".$data_key."' AND `Name` = '".$userdata['user_name']."'"); if (mysql_num_rows($query) == 0) { print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"apply\" VALUE=\"Apply\">"."</td>"."</tr>\n"; } else { print "<tr>"."<td>"."<INPUT TYPE=\"SUBMIT\" name=\"unapply\" VALUE=\"Unapply\">"."</td>"."</tr>\n"; } ?> Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted October 28, 2007 Share Posted October 28, 2007 Using the mysql COUNT() function is quicker than the php mysql_num_rows() function Quote Link to comment Share on other sites More sharing options...
kevincro Posted October 28, 2007 Author Share Posted October 28, 2007 Thanks a lot guys, that worked perfectly. Quote Link to comment 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.