Need help reffering to items from "mysql_fetch_array()"

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
TeamCIT
Forum Newbie
Posts: 1
Joined: Thu Jan 21, 2010 8:46 am

Need help reffering to items from "mysql_fetch_array()"

Post by TeamCIT »

Hey guys, I'm new here. 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:

Code: Select all

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.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Need help reffering to items from "mysql_fetch_array()"

Post by social_experiment »

Code: Select all

 
<?php
 $result = mysql_query("SELECT dest_addr  FROM destinations WHERE selected = '1' ");
 //
 while ($row = mysql_fetch_array($result)) {
  /*
   some code here 
  */
 }
 //
 switch ($counter) {
   case 1:
   echo "http://maps.google.com/maps?f=d&source=s_d";
   echo "&saddr=";
   echo $row[0];
   break;
   /*
    rest of switch statement
   */
 }
?>
 
In your query you are selecting a field called 'dest_addr', but in your switch statement you are refering to $row[0], $row[1], etc. If you select a field from a table, only that field will be available to you when you wish to print / echo results. Could you please show me what your db table looks like?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: Need help reffering to items from "mysql_fetch_array()"

Post by JakeJ »

If you know ahead of time what the prefixes are, you could create a prefix table and then store the key to the prefix with the address in your address table.

Table: prefixes
id
prefix

Table: addresses
id
prefix_id (refers to prefixes.id)
address
Post Reply