I'm really struggling (always do with arrays). What I want to do is pull the latest 10 news stories, split them in 2, add an advert and then put them into a JSON array ready to send to an iPhone. So far I have this (messy, I know): $count = 0; foreach ($rows AS $row){ $count++; //echo $row[id]."<br />"; $result_array[] = $row[id]; if ($count == 5){ $count = 0; //echo "Advert<br />"; $ad = array("newad.jpg"); $array = array_merge($result_array, $ad); $message = $message = array( "Stories" => array (array( "success" => 'true', "rows" => $array ))); echo json_encode($message); } PHP: Expected Output: {"Stories":[{"success":"true","rows":["964","963","960","959","958","newad.jpg","959","960","961","962","963"]}]} PHP: Output {"Stories":[{"success":"true","rows":["964","963","960","959","958","newad.jpg"]}]}{"Stories":[{"success":"true","rows":["964","963","960","959","958","957","956","955","954","952","newad.jpg"]}]} PHP: I can see I'm going wrong in the if ($count == 5) as that's where it's adding it as a new array rather than appending the existing array. If somebody could help me clean this up and outputting the expected result, that would be great!
You're overcomplicating things. Here: <?php $rows = array(1,2,3,4,5,6,7,8,9,10); $count = 0; $result_array = array(); foreach ($rows as $row) { $count++; $result_array[] = $row; if ($count == 5) { $result_array[] = 'newad.jpg'; } $message = $message = array( "Stories" => array( "success" => 'true', "rows" => $result_array ) ); } echo json_encode($message); ?> PHP: outputs: {"Stories":{"success":"true","rows":[1,2,3,4,5,"newad.jpg",6,7,8,9,10]}} Make a note that I've changed the code above to work with the provided sample-array, hence you need to change $row to $row['id']; and perhaps make other adjustments
Thanks, almost works as expected. I was using $row['id'] so I could see the data more easily. I do want the entire rows outputting. Quick one though, any reason you can think of why body would be returning null? Here is what I am seeing: Full code: $sth = $dbconn->prepare(" SELECT * FROM `stories` INNER JOIN images ON stories.id = images.story_id WHERE cat_id =:catid ORDER BY stories.id DESC LIMIT 10 "); $sth->execute(array(":catid"=> ''.$_GET[catid].'')); $result_array = array(); $rows = $sth->fetchAll(PDO::FETCH_ASSOC); $count = 0; $result_array = array(); foreach ($rows as $row) { $count++; $result_array[] = $row; if ($count == 5) { $result_array[] = 'newad.jpg'; } $message = $message = array( "Stories" => array(array( "success" => 'true', "rows" => $result_array ) ) ); } echo json_encode($message); PHP:
No, sorry - what should the "body" contain? Perhaps more of the tables have a "body" field? Without seeing the actual data in the database, it's almost impossible to discern what might be wrong.
Nope, thought it had worked but it's outputting it as an array rather than JSON... EDIT ======== I had to encode the row in UTF-8 before adding it to the JSON array. Final code: $sth = $dbconn->prepare(" SELECT * FROM `stories` INNER JOIN images ON stories.id = images.story_id WHERE cat_id =:catid ORDER BY stories.id DESC LIMIT 10 "); $sth->execute(array(":catid"=> ''.$_GET[catid].'')); $result_array = array(); $rows = $sth->fetchAll(PDO::FETCH_ASSOC); $count = 0; $result_array = array(); foreach ($rows as $row) { $count++; $result_array[] = array_map('utf8_encode', $row); if ($count == 5) { $advert = base64_encode(file_get_contents('http://www.cheshireindependent.co.uk/app/images/ads/advert.gif')); $result_array['Advert'] = $advert; } $message = $message = array( "Stories" => array(array( "success" => 'true', "rows" => $result_array ) ) ); } echo json_encode($message); PHP:
I would use built in PHP to do the inner stuff. In regards to the array showing multiple times it is because you have the $message = .. in the actual loop. $sth = $dbconn->prepare(" SELECT * FROM `stories` INNER JOIN images ON stories.id = images.story_id WHERE cat_id =:catid ORDER BY stories.id DESC LIMIT 10 "); $sth->execute(array(":catid"=> ''.$_GET[catid].'')); $result_array = array(); $result_array = $sth->fetchAll(PDO::FETCH_ASSOC); array_walk_recursive($result_array, 'utf8_encode'); array_splice($result_array, 5, 0, array(base64_encode(file_get_contents('http://www.cheshireindependent.co.uk/app/images/ads/advert.gif')))); $message = array( "Stories" => array( array( "success" => 'true', "rows" => $result_array ), ), ); echo json_encode($message); PHP:
Thanks, looks a lot neater and would obviously take less memory but it outputs like so: {"Stories":[{"success":"true","rows":[""]}]} PHP:
You've got a LOT of extra variables and 'code for nothing' in there. $statement = $dbconn->prepare(' SELECT * FROM stories INNER JOIN images ON stories.id = images.story_id WHERE cat_id = :catid ORDER BY stories.id DESC LIMIT 10 '); $statement->execute([ ':catid' => $_GET[catid] ]); $results = []; $count = 0; while ($row = $statement->fetch(PDO::FETCH_ASSOC)) { $results[] = array_map('utf8_encode', $row); if ((++$count) == 5) $results['Advert'] = base64_encode( file_get_contents('http://www.cheshireindependent.co.uk/app/images/ads/advert.gif') ); } echo json_encode([ 'Stories' => [ 'success' => 'true', 'rows' => $results ] ]); Code (markup): Though I'd REALLY REALLY NOT send the contents of the gif for the advert, waste of bandwidth if it gets sent more than once -- why aren't you just sending it's URL?
Thanks, will test that and let you know. As per the image, yep I'd already changed that to only have the URL.