SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Iterating over multiple value fields for a single column

    My sql database has a table called address. The names of two of its fields are latitude and longitude. When I iterate through the results using the while loop and a foreach loop, I get all of the values of each field associated with its corresponding column. However, I do not see how to get each value separately because each value is associated with the same key (either a number if I use mysqli_fetch_array or a string if I use mysqli_fetch_assoc), i.e. - latitude or longitude.
    My question is: How do I get to the separate values so that I can work with them?

    Below is a snippet of the relevant code:

    PHP Code:
    <?php
        
        
    // use returned data // onclick send to google // have results sent to maps page
        
    while($coords mysqli_fetch_array($result)) {
        
        
        foreach (
    $coords as $key => $val) {
            
            echo 
    "<pre>$key = $val <br /></pre>";
            
        }    
        
         
    ?>
    Below are snapshots of my results:
    array
    Screen Shot 2014-02-14 at 4.17.07 PM.png

    assoc
    Screen Shot 2014-02-14 at 4.19.50 PM.png

    Thank you for any time you might have to look at this.

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,617
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    you can use fetch() to get one row at a time, rather than parsing the entire set of results.

    http://www.php.net/manual/en/mysqli-stmt.fetch.php

    The entire set of results includes two sets of columns--one with the column names and one with the column indexes.

    $row = mysqli_fetch_array($result, MYSQLI_NUM); will return just the results with column indexes (most of the time, this is not needed).

    $row = mysqli_fetch_array($result, MYSQLI_ASSOC); will return just the results with the column names (this is probably what you're looking for if you want the results all at once).

    http://us3.php.net/mysqli_fetch_array
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Am I missing something? You would just use the keys to get these values.
    PHP Code:
        while($coords mysqli_fetch_array($resultMYSQLI_ASSOC)) {
            
    $latitude $coords['latitude'];   
            
    $longitude $coords['longitude'];        
            echo 
    "<pre>Latitude: $latitude = Longitude: $longitude <br /></pre>"
        } 

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,870
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Drummin View Post
    Am I missing something? You would just use the keys to get these values.
    PHP Code:
        while($coords mysqli_fetch_array($resultMYSQLI_ASSOC)) {
            
    $latitude $coords['latitude'];   
            
    $longitude $coords['longitude'];        
            echo 
    "<pre>Latitude: $latitude = Longitude: $longitude <br /></pre>"
        } 
    You don't even need the $lattitude and $logitude fields as you can reference $coords['latitude'] and $coords['longitude'] directly from the echo statement.

    If you are going to use the extra fields then you should be sanitizing the values as a part of the assignment. Both fields should be numbers so anything that isn't valid in a number should be stripped out. Of course if the database fields are one of the numeric formats then this is unnecessary.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    772
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Ya, ya... Ha ha. Got me again. He was echoing variables, so I echoed variables. He should only query fields he needs. How he formats the output I guess depends on what it's used for. Does he need to build a specific array to use with mapquest, just echo results, compare one to another etc.

  6. #6
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the reply. I apologize, but I think my question was unclear in its intent. I have revamped the question and started a new thread.

  7. #7
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,268
    Mentioned
    196 Post(s)
    Tagged
    2 Thread(s)


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •