Jump to content

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>";
?>		

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.