Jump to content

[SOLVED] Array problem


DeanWhitehouse

Recommended Posts

I have data stored in my database as such (this is only one table, one field in the whole db)

array("left" => array("article_1" => array("id" => 1,"pos" => 2),"article_2" => array("id" => 2,"pos" => 1),"article_3" => array("id" => 3,"pos" => 3)))

 

when i try and extract it doing

<?php
$newspaper = mysql_fetch_assoc($newspaper);
?>
<h1>American Gangsters Newspaper - <?=Secure($newspaper['title'])?></h1>
<table align="center" width="100%">
	<tr>
		<td colspan="2" align="center" style="border-bottom:thin solid grey;">
			<h2>Issue Number: <?=number_format($newspaper['id'])?>, Published <?=ConvertTimeToWords($newspaper['time'])?></h2>
		</td>
	</tr>
	<tr>
		<td colspan="2" align="left" style="border-bottom:thin solid grey;">
			Editor <?=CreateUserLink($newspaper['editor_id']);?><br>
			Description:<br> <?=Secure($newspaper['desc'])?>
		</td>
	</tr>
	<?php
	$left = $newspaper['articles']['left'];
	$right = $newspaper['articles']['right'];
	?>
	<tr>
		<td align="left" width="50%" style="border-right:thin solid grey;">
			<?php print_r($newspaper['articles']); echo $left;?>
			<h3></h3>
			Article OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle One
			<hr>
			<h3>Article Title</h3>
			Article OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle OneArticle One
		</td>
		<td align="left" width="50%">
			Article Two
		</td>
	</tr>
	<tr>
		<td colspan="2" align="left" style="border-top:thin solid grey;">
			<h2>Adverts</h2>
			<?=Secure($newspaper['advert'])?>
		</td>
	</tr>
</table>

I get an unexpected result, the print_r($newspaper['articles']); prints the array as expected but echo $left; prints the letter a

:s

 

Same if i try print_r($left);

 

Any ideas?

 

Thanks,

Blade

Link to comment
https://forums.phpfreaks.com/topic/167177-solved-array-problem/
Share on other sites

array("left" => array("article_1" => array("id" => 1,"pos" => 2),"article_2" => array("id" => 2,"pos" => 1),"article_3" => array("id" => 3,"pos" => 3)))

 

Thats what print_r($newspaper['articles']);

prints

 

The data stored in the db is

array("articles" => array("left" => array("article_1" => array("id" => 1,"pos" => 2),"article_2" => array("id" => 2,"pos" => 1),"article_3" => array("id" => 3,"pos" => 3))),array("right" => array("")))

Link to comment
https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881907
Share on other sites

array("left" => array("article_1" => array("id" => 1,"pos" => 2),"article_2" => array("id" => 2,"pos" => 1),"article_3" => array("id" => 3,"pos" => 3)))

 

Thats what print_r($newspaper['articles']);

prints

 

No, I don't think it is. That's not the format that print_r() uses when displaying it's output.

Link to comment
https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881918
Share on other sites

I have tried using eval already it created a wierd error

 

Using your way it creates these errors

08:14 am:

Notice: Array to string conversion in /home/vheissu/public_html/paper.php on line 42

08:14 am:

Parse error: syntax error, unexpected $end, expecting '(' in /home/vheissu/public_html/paper.php(42) : eval()'d code on line 1

 

trying

	$left = eval($newspaper['articles']);
	$left = $left['left'];

I get

08:14 am:

Parse error: syntax error, unexpected $end, expecting '(' in /home/vheissu/public_html/paper.php(42) : eval()'d code on line 1

Link to comment
https://forums.phpfreaks.com/topic/167177-solved-array-problem/#findComment-881930
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.