0

I want to add two values from two rows to an array. One is a path and the other is a tag. I'm then encoding the array. So I can display the image and then I'm going to using the tags to order the new javascript array dynamically. I would like "echo $data;" to say something like [{path, tag}, {path, tag}]

<?php

include("mysqlconnect.php");

$select_query = "SELECT `ImagesPath`,`Tag` FROM `offerstbl` ORDER by `ImagesId` DESC";
$sql = mysql_query($select_query) or die(mysql_error());   
$data = array();
while($rows = mysql_fetch_array($sql,MYSQL_BOTH)){
$data[] = $rows['ImagesPath']['Tag'];
}
echo json_encode($data);
echo $data[0];
?>
2
  • what value u get while echo Commented Aug 27, 2014 at 14:33
  • ["h","h","h","h","h"] for the full echo and h for the single one. This makes no sense to me. h doesn't represent anything in the database Commented Aug 27, 2014 at 14:35

1 Answer 1

1

I'm going to guess that "h" is the first character in the ImagePath string that is being returned from $rows['ImagesPath']['Tag']. I'm guessing that $rows['ImagePath'] is a string and you've asked for the ['Tag'] key within that string. Which isn't found for obvious reasons and the first character is then returned.

Since ImagePath and Tag are separate columns in the table you can't access both simultaneously. If you want the output to be [{path, tag}, {path, tag}] then you will need to change:

$data[] = $rows['ImagesPath']['Tag'];

to

$data[] = array($rows['ImagesPath'], $rows['Tag']);
Sign up to request clarification or add additional context in comments.

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.