Jump to content

Need help reffering to items from "mysql_fetch_array()"


Recommended Posts

Hey guys, I would be very greatful if any of you could help me with this problem.  I am using the "mysql_fetch_array()" to pull addresses I have stored in a database.  I am then trying to use these addresses along with PHP code to generate a link to google maps, to give directions to the chosen addresses.  The problem I am having is trying to refer to the addresses individually once they have been pulled from my database.  The reason I need to do this is because google maps uses a different prefix for the first two addresses, then the same prefix for every address after that. Any help or tips are greatly appreciated, thanks in advance.

 

Here is the code I am using that is not working as desired:

if($_POST['linkgen'] == true) {
$result = mysql_query("SELECT dest_addr 
			FROM destinations
			WHERE selected = '1'");			

$counter = mysql_num_rows($result); 

echo "<a href=\"";
echo "http://maps.google.com/maps?f=d&source=s_d";
while ($row = mysql_fetch_array($result)) {

		switch($counter) {
	case 1:
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		break;
	case 2:
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		break;		
	case 3:
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		break;
	case "4":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		break;
	case "5":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		break;
	case "6":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		break;
	case "7":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		echo "&mrad=";
		echo $row[6];
		break;
	case "8":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		echo "&mrad=";
		echo $row[6];
		echo "&mrad=";
		echo $row[7];
		break;
	case "9":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		echo "&mrad=";
		echo $row[6];
		echo "&mrad=";
		echo $row[7];
		echo "&mrad=";
		echo $row[8];
		break;
	case "10":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		echo "&mrad=";
		echo $row[6];
		echo "&mrad=";
		echo $row[7];
		echo "&mrad=";
		echo $row[8];
		echo "&mrad=";
		echo $row[9];
		break;
	case "11":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		echo "&mrad=";
		echo $row[6];
		echo "&mrad=";
		echo $row[7];
		echo "&mrad=";
		echo $row[8];
		echo "&mrad=";
		echo $row[9];
		echo "&mrad=";
		echo $row[10];
		break;
	case "12":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		echo "&mrad=";
		echo $row[6];
		echo "&mrad=";
		echo $row[7];
		echo "&mrad=";
		echo $row[8];
		echo "&mrad=";
		echo $row[9];
		echo "&mrad=";
		echo $row[10];
		echo "&mrad=";
		echo $row[11];
		break;
	case "13":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		echo "&mrad=";
		echo $row[6];
		echo "&mrad=";
		echo $row[7];
		echo "&mrad=";
		echo $row[8];
		echo "&mrad=";
		echo $row[9];
		echo "&mrad=";
		echo $row[10];
		echo "&mrad=";
		echo $row[11];
		echo "&mrad=";
		echo $row[12];
		break;
	case "14":
		echo "http://maps.google.com/maps?f=d&source=s_d";
		echo "&saddr=";
		echo $row[0];
		echo "&daddr=";
		echo $row[1];
		echo "&mrad=";
		echo $row[2];
		echo "&mrad=";
		echo $row[3];
		echo "&mrad=";
		echo $row[4];
		echo "&mrad=";
		echo $row[5];
		echo "&mrad=";
		echo $row[6];
		echo "&mrad=";
		echo $row[7];
		echo "&mrad=";
		echo $row[8];
		echo "&mrad=";
		echo $row[9];
		echo "&mrad=";
		echo $row[10];
		echo "&mrad=";
		echo $row[11];
		echo "&mrad=";
		echo $row[12];
		echo "&mrad=";
		echo $row[13];
		break;
	default:
		break;
		}
}
echo "\">" . "Directions" . "</a>";
}

 

Also, any tips on making my code more efficient are more than welcome.

If you need to so something different on the first piece if data in a loop, you initialize a counter or a flag to zero (or one if you prefer) or a false value before the start of the loop. You test the value of the counter/flag in the loop to perform the special processing and you increment the counter or set the flag to a true value in the loop so that the special processing is only done once.

If you need to so something different on the first piece if data in a loop, you initialize a counter or a flag to zero (or one if you prefer) or a false value before the start of the loop. You test the value of the counter/flag in the loop to perform the special processing and you increment the counter or set the flag to a true value in the loop so that the special processing is only done once.

 

Thanks for your input. This does sound like a solution that will work, however, I'm not sure where I would put the counter since I am using a switch case statement. Oh and if it matters, the first piece of data will have a unique prefix, and the second piece will use a different unique prefix.  Any ideas?

if($_POST['linkgen'] == true) {
   $result = mysql_query("SELECT dest_addr
            FROM destinations
            WHERE selected = '1'");         
   
   $counter = 1;
   
   echo '<a href="http://maps.google.com/maps?f=d&source=s_d';
   while ($row = mysql_fetch_assoc($result)) {
         
         switch($counter) {
      case 1:
         echo "&saddr=";
         break;
      case 2:
         echo "&daddr=";
         break;      
      default:
	 echo "&mrad=";	  
         break;
         }
echo $row['dest_addr'];
$counter++;
   }
   echo '">Directions</a>';
}

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.