0

I am trying to display more than 1 image, if it is required.

I am not asking for a spoon-feed, but to be put in the right direction.

My current echo to print the database column:

    <table style="margin:0 5%;border-left: 1px solid #000;" id="monsterstable" cellpadding="4" cellspacing="0" width="90%"><thead> <tr>
  <th class="tabletop" width="150">NPC Picture</th>
  <th class="tabletop">NPC Name </th>
<th class="tabletop">NPC Drops</th>
</tr></thead> 
<?php
    $query = $_GET['query']; 
    // gets value sent over search form

    $min_length = 3;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM npcs
            WHERE (`npc_name` LIKE '%".$query."%')") or die(mysql_error());

        // * means that it selects all fields, you can also write: `id`, `title`, `text`
        // articles is the name of our table

        // '%$query%' is what we're looking for, % means anything, for example if $query is Hello
        // it will match "hello", "Hello man", "gogohello", if you want exact match use `title`='$query'
        // or if you want to match just full word so "gogohello" is out use '% $query %' ...OR ... '$query %' ... OR ... '% $query'

        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

             echo '<form action="search.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
    </form>';
             echo "<br>Showing results for <b>'".$query."'</b><br>";
                echo '<tr><td class="tablebottom"><img src="'.$results['image'].'" width="100px" height="100px""></td><td class="tablebottom">'.$results['npc_name'].'</td>
<td class="tablebottom"><img src="'.$results['location'].'" width="20px" height="20px"></td></tr>';
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }

        }
        else{ // if there is no matching rows do following
            echo 'No results<br><form action="search.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
    </form>';

        }

    }
    else{ // if query length is less than minimum
        echo '<form action="search.php" method="GET">
        <input type="text" name="query" />
        <input type="submit" value="Search" />
    </form><br><br>Minimum length is '.$min_length.'<br>';

    }
?>
</table>

And my SQL:

https://i.stack.imgur.com/XEquI.png

How do I add multiple images and get it to echo them?

Ikov PS
  • 1
  • 4
  • Without any more context, all I can say is to use a loop. Presumably you're querying the database to echo out the one image - why can't you add a loop at that point? – andrewsi Apr 15 '16 at 23:44
  • Question would be, how would i get it to loop through 1 row? Do i seperate the images with a " , " in the field or? – Ikov PS Apr 15 '16 at 23:45
  • You'd normally have multiple rows in the database, rather than using multiple entries in the same field. That's just going to get messy – andrewsi Apr 15 '16 at 23:47
  • Don't use commas, http://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad. 1 row per record/data piece. – chris85 Apr 15 '16 at 23:48
  • Could you give me an example for this? i am using a search bar , Located : http://agora-pk.com/drops/search.php , use the word " demon ". – Ikov PS Apr 15 '16 at 23:48
  • Updated main post for full code – Ikov PS Apr 15 '16 at 23:53
  • Please dont use [the `mysql_` database extension](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php), it is deprecated (gone for ever in PHP7) Especially if you are just learning PHP, spend your energies learning the `PDO` or `mysqli_` database extensions, [and here is some help to decide which to use](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – RiggsFolly Apr 15 '16 at 23:53
  • Still have no clue how to echo more images from 1 row – Ikov PS Apr 16 '16 at 00:11

1 Answers1

0

You're going to want to loop through your MySQL rows and echo the entire img tag with each one.

foreach($results as $result){
    echo "<img src='$results->image' width='20px' height='20px'>";
}

Adjusting for how you're already looping through results, whether you're using PDO or Mysqli or whatnot. This assumes you're getting an array of results called $results with each element containing a row object, with key image containing your image url.

EDIT:

So then, something like this, if I'm interpreting your data structures correctly:

echo '<form action="search.php" method="GET">
            <input type="text" name="query" />
            <input type="submit" value="Search" />
        </form>';
echo "<br>Showing results for <b>'".$query."'</b><br>";

while($results = mysql_fetch_array($raw_results)){
    echo '<tr>
        <td class="tablebottom">
            <img src="'.$results['image'].'" width="100px" height="100px">
        </td>
        <td class="tablebottom">'.$results['npc_name'].'</td>
        <td class="tablebottom">
            <img src="'.$results['location'].'" width="20px" height="20px">
        </td>';
}

echo '</tr>';

    // posts results gotten from database(title and text) you can also show id ($results['id'])
}

Although, as stated by others, mysql is deprecated in PHP and mysqli or PDO should be used instead.

Jacob See
  • 755
  • 6
  • 17