StefanRSA Posted April 23, 2009 Share Posted April 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/155311-explode-multiple-strings-and-display-arrays-in-a-table/ Share on other sites More sharing options...
Psycho Posted April 23, 2009 Share Posted April 23, 2009 <?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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155311-explode-multiple-strings-and-display-arrays-in-a-table/#findComment-817115 Share on other sites More sharing options...
avvllvva Posted April 23, 2009 Share Posted April 23, 2009 <?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] Quote Link to comment https://forums.phpfreaks.com/topic/155311-explode-multiple-strings-and-display-arrays-in-a-table/#findComment-817119 Share on other sites More sharing options...
StefanRSA Posted April 23, 2009 Author Share Posted April 23, 2009 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!!! Quote Link to comment https://forums.phpfreaks.com/topic/155311-explode-multiple-strings-and-display-arrays-in-a-table/#findComment-817123 Share on other sites More sharing options...
avvllvva Posted April 23, 2009 Share Posted April 23, 2009 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>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/155311-explode-multiple-strings-and-display-arrays-in-a-table/#findComment-817130 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.