Jump to content

loop problem part 2.


beansandsausages

Recommended Posts

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 ?

Link to comment
https://forums.phpfreaks.com/topic/86294-loop-problem-part-2/
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/86294-loop-problem-part-2/#findComment-440845
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/86294-loop-problem-part-2/#findComment-440847
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.