0

I want to add these images from my server to an JavaScript array so I can work with them.

The example below should alert the number of images in the folder. At the moment there is two images in there and this script alerts me twice saying I've the length of the array is one. Obviously I want one alert saying two.

<?php
    include("mysqlconnect.php");

    $select_query = "SELECT `ImagesPath` FROM `offerstbl` ORDER by `ImagesId` DESC";
    $sql = mysql_query($select_query) or die(mysql_error());   
    while($row = mysql_fetch_array($sql,MYSQL_BOTH)) {
?>

<script> var images = new Array("<?php echo $row["ImagesPath"]; ?>"); 
alert(images.length);
5
  • 3
    json_encode() Commented Aug 6, 2014 at 0:55
  • totally json_encode(). DONT EVER Mix php with javascript like your doing there. its cheap, and it'll kick you in the end Commented Aug 6, 2014 at 0:56
  • I've tried json encode() found it difficult to corporate mine into examples. Commented Aug 6, 2014 at 0:59
  • “At the moment there is two images in there and this script alerts me twice saying I've the length of the array is one.” – that’s because you are outputting that JavaScript code once for each record, with the array only containing one record’s image path each time. Put the values into a (PHP) array first, and then use json_encode on that: var images = <?php echo json_encode($yourArray); ?>; Commented Aug 6, 2014 at 1:03
  • please please please, stop using mysql extension, it is deprecated and will be removed. migrate to mysqli or PDO Commented Aug 6, 2014 at 1:40

2 Answers 2

1

As scrowler pointed out in the comments, use json_encode(). What you're doing now is going to kill whatever you code. I can guarantee that. (If you don't believe me, just ask anybody here on Stack Overflow.)

It could simply be done like this:

$data = array();
while($row = mysql_fetch_array($sql,MYSQL_BOTH)){
    $data[] = $row['ImagesPath'];
}

See how we add the images to the $data array above? Now we can simply add this under the loop:

$images = json_encode($data);

That would give you that looks something like this:

{'path/to/image', 'path/to/another/image'}

I strongly advise against you doing this the way you are doing now, but to answer to the scope of your question, your end code would look something like this:

<?php
//....all the query stuff etc...
$data = array();
while($row = mysql_fetch_array($sql,MYSQL_BOTH)){
    $data[] = $row['ImagesPath'];
}

$images = json_encode($data);
?>

<script>var images = <?php echo $images; ?></script>
Sign up to request clarification or add additional context in comments.

Comments

0
<?php 
include("mysqlconnect.php");
 $select_query = "SELECT `ImagesPath` FROM `offerstbl` ORDER by `ImagesId` DESC";
 $sql = mysql_query($select_query) or die(mysql_error());
 $rows = mysql_fetch_assoc($sql);
 echo "<script> var images = array();"
 for($i=0;$i<count($rows);$i++)
{
 $current_image = $rows[0];
echo "images.push($current_image)";
}
?>

This code will push all the images_paths into images array using for loop. It will flush long code for more images but will work fine. If you do not want to flush a long js file then tell me , i will also do it.

Comments

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.