Jump to content

printing array output


jimfog

Recommended Posts

I am using a foreach loop to print the contents of an array to the browser:

<?php $serviceslist=get_services($conn,$_SESSION['valid_user']);                           foreach ($serviceslist as  $value )                                    {                                     print_r($value);                                     echo '<br>';                                    }?>   

the above array lists the contents from 2 columns(services,price)...all in all, there are 4 values and the problem is that due to the br tag the values are listed one below another.

 

What I want is a table structure that resembles the table. I want the data printed in the browser rows after rows,

For example:

service price

service price.

 

Now it is like that:

service

price

service

price.

 

How I will fix it? I think it s clearly an HTML problem

Link to comment
Share on other sites

you should restructure your array to another dimension.

$service['price']="foo"; $service['name"]="bar";foreach ($serviceslist as $value ){echo $value['price'] . $value['name'];echo '<br>';}?> 

Also if you want to use print data proper order using print_r(), you should use the second parameter.

http://php.net/print_r

Edited by birbal
Link to comment
Share on other sites

take a look at the code...it gives me Warning: Illegal string offset 'services' :

$serviceslist['services']=$services->servicename;
foreach ($serviceslist as $value )
{
echo $value['services'].'<br>';
}

Ι cannot understand why this is hapenning.

Link to comment
Share on other sites

That means $value is a string. You weren't meant to just copy the code he gave you, he said to restructure your arrays.

An example that corresponds to the word "restructure" would help here.The obvious thing was to follow the example given...and so I did.

Link to comment
Share on other sites

It seems you still need to learn more about arrays and the foreach loop.

 

This line of your code creates one element in the $serviceslist array called "services"

$serviceslist['services']=$services->servicename;

The foreach loop in your code assigns the value of $serviceslist['services'] to the variable $value, so $value is a string and $serveiceslist only has one element.

 

Birbal's code will work but your array needs to be structured as follows:

 

$serviceslist[0] = array(    'price' => 'X',    'name' => 'Y');$serviceslist[1] = array(    'price' => 'X',    'name' => 'Y');$serviceslist[2] = array(    'price' => 'X',    'name' => 'Y');$serviceslist[3] = array(    'price' => 'X',    'name' => 'Y');

 

A shorthand way to add new elements onto the end of an array without having to know what numeric index to use is the following:

 

$a[] = "value";

Given that knowledge, the previous code can be written as:

 

$serviceslist = array();$serviceslist[] = array(    'price' => 'X',    'name' => 'Y');$serviceslist[] = array(    'price' => 'X',    'name' => 'Y');$serviceslist[] = array(    'price' => 'X',    'name' => 'Y');$serviceslist[] = array(    'price' => 'X',    'name' => 'Y');

It doesn't seem extremely useful at the moment, but it's exceptionally useful when getting values from a loop, for example databases.

 

$serviceslist = array();while ($row = mysqli_fetch_assoc()) {    $serviceslist[] = array(        'price' => $row['price'],        'name' => $row['name']    )}
Link to comment
Share on other sites

In other words you are suggesting me to use a multi-dimensional array. Is that what the above is called?Just to be certain.

Link to comment
Share on other sites

I CANNOT BELIEVE IT I FINALLY MADE IT TO WORK. :good:

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...