Jump to content

Explode multiple strings and display arrays in a table


StefanRSA

Recommended Posts

Hi, I get strings from a field in a MySql Table like:

$row1 = mysql_fetch_array( $atable );
$avail = $row1['avail'];

$avail can give me the following:

Single,2,2,0,0, YES,ZAR1480;

Double,2,2,2,0, NO, ZAR1480;

 

I can Explode $avail with:

$gresult = explode(";",$avail);

I can split each array further individually with

$unit_result= explode(",",$gresult[0]);
$unit_result= explode(",",$gresult[1]);

 

But how can I split it so I can display the following in a table in a form as the $avail might contain 1 - 18 arrays in $unit_result?

 

I want it to display as follow like the example below.....:

 

|=====================================================|

|Type      |Units      |Adults    |Children  |Minors | Availability | Price  |

|--------------------------------------------------------------------|

|Single    |2            |2          |0          |0        |Yes          |ZAR1480|

|--------------------------------------------------------------------|

|Double    |2            |2          |2          |0        |No            |ZAR1480|

|--------------------------------------------------------------------|

 

I tried a while statement but don't come right.....

I am new to php so any help would really be appreciated

 

Thanks

Stefan

 

<?php

$row1 = mysql_fetch_array( $atable );
$avail = $row1['avail'];

echo "<table>\n";
echo "<tr>\n";
echo "<th>Type</th>\n";
echo "<th>Units</th>\n";
echo "<th>Adults</th>\n";
echo "<th>Children</th>\n";
echo "<th>Minors</th>\n";
echo "<th>Availability</th>\n";
echo "<th>Price</th>\n";
echo "</tr>\n";

$recordset = explode(';',$avail);

foreach ($recordset as $record)
{
    $datarow = explode(',', $record);
    foreach ($datarow as $value)
    {
        echo "<tr>\n";
        echo "<td>{$data[0]}</td>\n";
        echo "<td>{$data[1]}</td>\n";
        echo "<td>{$data[2]}</td>\n";
        echo "<td>{$data[3]}</td>\n";
        echo "<td>{$data[4]}</td>\n";
        echo "<td>{$data[5]}</td>\n";
        echo "<td>{$data[6]}</td>\n";
        echo "</tr>\n";
    }
}

echo "</table>\n";

?>

<?php 
$avail 	= "Single,2,2,0,0, YES,ZAR1480;Double,2,2,2,0, NO, ZAR1480;";
$table 	= "<table border='1'>";
$table .= "<tr><td>Type</td><td>Units</td><td>Adults</td><td>Children</td><td>Minors</td><td>Availability</td><td>Price</td></tr>";
$gresult = explode(";",$avail);
foreach ($gresult as $x ){
    $table .= "<tr>";
 	 $unit_result= explode(",",$x);
 	 foreach ($unit_result as $y ){
	    $table .= "<td>$y</td>";
	 }
$table .= "</tr>";
}
echo $table .= "</table>";
?>		/code]

Hi Guys!

 

Thanks for the fast response... I have two problems...

Mjdamato, your answer gives me a table with only the headings but all the rows thereafter is empty???

Avvllvva, your answer gives me the full result table with one extra row with one cell that is empty?

 

Why is that?

 

Thanks again! You guys are great!!!

Your string contains semicolon(the string separator) twice, so its showing one extra row.

Solution is either remove the last semicolon or put an empty checking condition like below

<?php 
$avail 	= "Single,2,2,0,0, YES,ZAR1480;Double,2,2,2,0, NO, ZAR1480;";
$table 	= "<table border='1'>";
$table .= "<tr><td>Type</td><td>Units</td><td>Adults</td><td>Children</td><td>Minors</td><td>Availability</td><td>Price</td></tr>";
$gresult = explode(";",$avail);
foreach ($gresult as $x ){
  if(!empty($x)){
$table .= "<tr>";
 	 $unit_result= explode(",",$x);
 	 foreach ($unit_result as $y ){
	    $table .= "<td>$y</td>";
	 }
$table .= "</tr>";
  }
}
echo $table .= "</table>";
?>		

 

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.