Forum Moderators: coopster

Message Too Old, No Replies

Unserialize An Echo Statement

Array List

         

oceanwave

10:25 pm on Aug 29, 2007 (gmt 0)

10+ Year Member



Hi,

Heat =&nbsp; <?php echo $home['heat'];?><br>

Displays the array in my database that needs to be unserialized.

Heat =&nbsp; <?php echo unserialize($home['heat']);?><br>

Displays the word "Array"

How do I write the code line so that I can see the unserialized array written as a list on my page?

Thank you.

jatar_k

11:24 pm on Aug 29, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try using print_r instead of echo

oceanwave

11:49 pm on Aug 29, 2007 (gmt 0)

10+ Year Member



Hi jatar_k, nice to see you again!

I replaced echo with print_r and got "Parse error: syntax error, unexpected T_VARIABLE.."

Don't know if this helps...array at top of page is in format of:

'heat' => '',

eeek

11:52 pm on Aug 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



print_r is a function. Put parens around its arguments.

oceanwave

12:12 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Thank you for responding eeek.

Heat =&nbsp; <?php print_r unserialize($home['heat']);?><br>

Gave me the error -

Parse error: syntax error, unexpected T_STRING in...

jatar_k

12:49 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



print_r(unserialize($home['heat']));

or you could do it in two steps

$myarray = unserialize($home['heat']);
print_r($myarray);

this may print out in a lump

are you looking for formatted output?

oceanwave

1:09 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Hi Jatar_k,

Thank you so much! Now I get:

Heat = Array ( [0] => Electric [1] => Gas [2] => Solar )

I would like this to look like an indented list (I added dots ..... because this forum took out my added spaces when I tried to post. I just want each item in the list to be indented):

.....Electric
.....Gas
.....Solar

jatar_k

2:07 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



maybe try implode [php.net] to join them together and format them

oceanwave

3:03 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Thanks again Jatar_k. I'm still not sure of what I need to do.

I thought it should be easy to pull an array from a database and unserialize it into a list. Maybe it isn't so easy.

There has to be a way to change Array ( [0] => Electric [1] => Gas [2] => Solar ) to just a list of the array values. I've been Googling for a couple of hours now, and can't seem to find what I am looking for.

jatar_k

3:09 am on Aug 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try something like this

$myarray = unserialize($home['heat']);
$mylist = implode('<br>',$myarray);
echo '<p>',$mylist;

oceanwave

3:25 am on Aug 30, 2007 (gmt 0)

10+ Year Member



Jatar_k, you did it again! Thank you so much. It works perfectly.

implode is a new one on me!

One last question...If there is nothing in the database for a particular array, I get:

"Warning: implode() [function.implode]: Bad arguments..."

What would I need to change in your code to make sure this message does not come up?

jatar_k

12:03 pm on Aug 30, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



test if anything is returned from the db

then put your implode inside an if statement

oceanwave

10:30 pm on Aug 30, 2007 (gmt 0)

10+ Year Member



Thank you jatar_k. I did as you instructed and it seems to be working.

Before I made the if/else change, my site was down twice due to exceeded cpu usage. Called my host and was told there was a query that took over 40 seconds. Could this have been caused by a php page being called that hadn't been created yet in my database (ex: mysite/page.php?id=39 when there are only 35 records in the database)? What I am thinking is that when I was testing the page I am creating and called a non-existant page (getting the Warning: implode() [function.implode]: Bad arguments.), it may have taken excessive time to query the database (though I can't remember anything taking over 40 seconds to process). Could this possibly be the reason for the cpu overage?