0

I am new to PHP and I am trying to print out 5 pictures to which the references are stored inside a string, separated by new line.

Here's what I have so far :

function displayTeam($heroes){  
    $herolist = array();

    $conn = odbc_connect('Dotalyzer', '', '');
    if (!$conn) {
        exit("Connection Failed: " . $conn);
    }
    $sql = "SELECT * FROM HeroStats ORDER BY Heroname";
    $rs = odbc_exec($conn, $sql);
    if (!$rs) {
        exit("Error in SQL");
    }
        foreach(preg_split("/((\r?\n)|(\r\n?))/", $heroes) as $line){
            while (odbc_fetch_row($rs)) {
                $heroname = odbc_result($rs, "HeroName");

                if ($line == $heroname) {

                    $heroimage = odbc_result($rs, "Portrait");
                    $herolist[] = $heroimage . ".png";
                }
            }
        }

    odbc_close($conn);

    echo " <div class='heroimage_wrapper'>";
    for ($i = 0; $i < 5; $i++) {
        echo " <img src='Hero icons/$herolist[$i]' class='heroimage'>";
    }
        echo " </div>";
}

I want to print each picture from the array, the references are stored in an Access database.

I am calling the function which generates my $heroes string using:

 $(document).ready(function(){
        $('.button').click(function(){
            var clickBtnValue = $(this).val();
            var id = $(this).attr('data-unique-id');
            var ajaxurl = 'ajax/ajax',
                data =  {
                    'action': clickBtnValue,
                    'id': id
                };
                console.log(clickBtnValue);
            $.post(ajaxurl, data, function (response) {
                // Response div goes here.
                alert(response.replace('\\n', '\n'));
            });
        });

    });
3
  • 3
    can you provide some output, what is the error you get, is there a response? what is the content of the header response? Commented Dec 30, 2014 at 21:07
  • @OussamaELGOUMRI in the alert I get the content of the $heroes string and then it prints out some HTML code followed by Undefined offset from 1 to 4 , for the img src Commented Dec 30, 2014 at 21:13
  • what is the name of your php file, and is ajax url pointing to the correct file? Commented Dec 30, 2014 at 21:21

2 Answers 2

1
  1. You can get 5 DB entities with LIMIT operation. Such as

    $query = 'SELECT * FROM HeroStats ORDER BY Heroname LIMIT 5'
    
  2. You can use mysqli to connect Database

    $db_connection = new mysqli([HOST], [USERNAME], [PASSWORD], [DATABASE_NAME]);
    
  3. Use mysqli::fetch_array to fetch images to an array. This function will help you

    function fetch($query)
    {
        $res = $db_connection->query($query);
        $array = [];
        while( $r = $res->fetch_array(MYSQLI_ASSOC) )
        {
            array_push($array, $r);
        }
    
        return $array;
    } 
    
  4. To pass array into Javascript, encode array to JSON or xml

    $array = fetch($query);
    $json = json_encode($array);
    
  5. So, you can receive this response in Javascript, just convert JSON to array

    var images = JSON.parse(response);
    
Sign up to request clarification or add additional context in comments.

2 Comments

can i connect to an MS Access database using mysqli ?
@BogdanMolinger, no, you can't, but you're didn't say it. If you have error Undefined index this means that array don't have element with such index. To debug Db result, please var_dump($heroes);die; after connection close
0

I can see two errors in your code.

First:

Change this

$herolist[] .= $heroimage . ".png";

to

$herolist[] = $heroimage . ".png"; 

Second:

Remove .png from img src attribute and update for loop to use array length

for ($i = 0; $i < 5; $i++) {
  echo " <img src='Hero icons/$herolist[$i].png' class='heroimage'>";
}

to

for ($i = 0; $i < count($herolist); $i++) {
   echo " <img src='Hero icons/$herolist[$i]' class='heroimage'>";
}

Also, you should not use spaces for folder names. Change your folder name from Hero icons to Hero_icons and update the img tag.

7 Comments

i've made the changes , now it prints out the correct path to the first hero from the team but not for the other ones , for example if first hero is a it prints <img src="Hero icons/a.png' class='heroimage'> but for the other ones only <img src="Hero icons/' class='heroimage'> and it says that it's an Undefined offset
Your query probably fetches only one image. Update the for loop as above.
The Undefined offset notices have disappeared but the images are still not printed , and it still prints out the code for the first image only
The query fetches only one image. Check your database and make sure it has all the images.
I'm not much familiar with Access database, but you can check the difference between the entry that shows and other entries and figure out why the conditions in your code don't satisfy.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.