www.webdeveloper.com
Results 1 to 9 of 9

Thread: MySQL into table using array - How to call back first column into an image?

  1. #1
    Join Date
    Jan 2013
    Posts
    11

    Smile MySQL into table using array - How to call back first column into an image?

    Hello,
    I'll start by explaining what I've got. Basically PHP calls the database (MySQL) and then put it into an html table.

    It works fine apart from I want the ID number to be placed into the image path. eg. ID 1 would call 1.png ID 2 calls 2.png etc.

    All i can manage to do is get the first letter of the Location's to be called.

    Many thanks and any help is appreciated greatly
    PS. I'm not the best at PHP

    PHP Code:
    <table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
        <thead>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Location</th>
                <th>Image</th>
                
            </tr>
        </thead>
        <tbody>
    <?php
    $db_host 
    'jsws.co.uk';
    $db_user 'jswscouk_snailad';
    $db_pwd 'erik123';

    $database 'jswscouk_snail';
    $table 'snail';

    if (!
    mysql_connect($db_host$db_user$db_pwd))
        die(
    "Can't connect to database");

    if (!
    mysql_select_db($database))
        die(
    "Can't select database");

    // sending query
    $result mysql_query("SELECT * FROM {$table}");
    if (!
    $result) {
        die(
    "Query to show fields from table failed");
    }

    $fields_num mysql_num_fields($result);
    /* printing table headers
    for($i=0; $i<$fields_num; $i++)
    {
        $field = mysql_fetch_field($result);
        echo "<thead><td>{$field->name}</td>";
    }*/
    echo "</tr>\n";
    // printing table rows
    while($row mysql_fetch_row($result))
    {
        echo 
    "<tr>";

        
    // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        
    foreach($row as $cell)
            echo 
    "<td>$cell</td>";
        echo 
    "<td><img src='???????' height='42' width='42'></td>";
        echo 
    "</tr>\n";
    }
    mysql_free_result($result);
    ?>
        </tbody>
    </table>
    Run on bootstrap if that makes any difference?

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    546
    What is the structure of your 'id' field? Use some kind of pattern recognition or position extraction to get that 'number' value and then use that and append ".png" to it to get your image file name. No?

  3. #3
    Join Date
    Jan 2013
    Posts
    11
    How would you suggest I incorporate that into the code already there?

    the ID's just go from 1 to 365 integer values
    thanks

  4. #4
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    546
    Change this:

    while($row = mysql_fetch_row($result))

    to
    while (list($id,$name,$loc) = MySQL_fetch_row($result))

    then
    remove the foreach and just output each of your td elements using the above 3 field names

    Create a field:

    $imgname = $id.'png';

    You might have to add a path to the front of that if the images are located somewhere else.

    Then output your last td element using $imgname

  5. #5
    Join Date
    Dec 2011
    Posts
    159
    actually it would be
    PHP Code:
    $imgname $id.".png"
    right?

  6. #6
    Join Date
    Jan 2013
    Posts
    11
    I found a much easier way of doing it...
    PHP Code:
    // printing table rows
    while($row mysql_fetch_row($result))
    {

        echo 
    "<tr>";
        
    // $row is array... foreach( .. ) puts every element
        // of $row to $cell variable
        
    foreach($row as $cell)
            echo 
    "<td>$cell</td>";
        
    $pic reset($row);
        echo 
    "<td><img src='$pic' height='60' width='60' ></td>";
        echo 
    "</tr>\n";

    The "reset($row)" resets the array to the start so it echos the first thing.

    Thanks for the help guys, if anyone knows any disadvantages to this then let me know

  7. #7
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    546
    Whatever works for you, but I don't see how you get the value of $pic. Where did that get set?

  8. #8
    Join Date
    Jan 2013
    Posts
    11
    $pic get set here:
    PHP Code:
    $pic reset($row); 
    It's resetting the array $row so it's pointed at the first element in the array again. So it returns the first element of the array. And then because it's within the while "($row = mysql_fetch_row($result)" it does it for each of the columns.

    I think...

    Source: http://php.net/manual/en/function.reset.php

  9. #9
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    546
    Yes - you're right. I never realized that the reset could return a value as well. I just thought it 'rewound' the array pointer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles