beansandsausages Posted January 16, 2008 Share Posted January 16, 2008 hey all. i have this bit of code : if($_GET['action'] == 'a' ) { $sql = "SELECT * FROM `contacts` where fname like 'a%' "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run " . mysql_error(); include_once("bottom.php"); exit; } if (mysql_num_rows($result) == 0) { echo "No contacts that start with <strong>A</strong> found in the database. Click <a href=\"?action=add\">here</a> to add a contact"; include_once("bottom.php"); exit; } echo "<table width=85%><td width=35%><u>Contact Name</u></td><td witdh=25%><u>Phone Number</u></td><td width=30%><u>Email Address</u></td></table> "; while ($row = mysql_fetch_assoc($result)) { $name = "".$row['fname'].", ".$row['lname'].""; echo "<table width=85%><td width=35%>".$name."</td><td witdh=25%>".$row['number']."</td><td width=30%>".$row['email']."</td> "; } } works fine but problem is i have it 26times for each lettor of the alebet. is there a way to do it in loops ? Quote Link to comment https://forums.phpfreaks.com/topic/86294-loop-problem-part-2/ Share on other sites More sharing options...
adam291086 Posted January 16, 2008 Share Posted January 16, 2008 you could set a variable to $_GET['action'] then do something like While(isset($variable)) { $sql = "SELECT * FROM `contacts` where fname like '$variable%' "; $result = mysql_query($sql); if (!$result) { echo "Could not successfully run " . mysql_error(); include_once("bottom.php"); exit; } if (mysql_num_rows($result) == 0) { echo "No contacts that start with <strong>A</strong> found in the database. Click <a href=\"?action=add\">here</a> to add a contact"; include_once("bottom.php"); exit; } echo "<table width=85%><td width=35%><u>Contact Name</u></td><td witdh=25%><u>Phone Number</u></td><td width=30%><u>Email Address</u></td></table> "; while ($row = mysql_fetch_assoc($result)) { $name = "".$row['fname'].", ".$row['lname'].""; echo "<table width=85%><td width=35%>".$name."</td><td witdh=25%>".$row['number']."</td><td width=30%>".$row['email']."</td> "; } } } Not a 100% sure though Quote Link to comment https://forums.phpfreaks.com/topic/86294-loop-problem-part-2/#findComment-440845 Share on other sites More sharing options...
cooldude832 Posted January 16, 2008 Share Posted January 16, 2008 well since using get you wanna make sure the data is valid so try <?php $letters= array("a","b","c","d,"...); if(in_arrary(strtolower($_GET['action']),$letters)){ $sql = "SELECT * FROM `contacts` where fname like '".strtolower($_GET['action'])."%' "; $result = mysql_query($sql) or die(mysql_error()."<br /><br />".$sql); if (mysql_num_rows($result) == 0) { echo "No contacts that start with <strong>A</strong> found in the database. Click <a href=\"?action=add\">here</a> to add a contact"; include_once("bottom.php"); exit; } echo "<table width=85%><td width=35%><u>Contact Name</u></td><td witdh=25%><u>Phone Number</u></td><td width=30%><u>Email Address</u></td></table> "; while ($row = mysql_fetch_assoc($result)) { $name = "".$row['fname'].", ".$row['lname'].""; echo "<table width=85%><td width=35%>".$name."</td><td witdh=25%>".$row['number']."</td><td width=30%>".$row['email']."</td> "; } } ?> Need to clean it up/finnish array but thats the idea Quote Link to comment https://forums.phpfreaks.com/topic/86294-loop-problem-part-2/#findComment-440847 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.