max_w1 Posted March 20, 2012 Share Posted March 20, 2012 I have a string (Product1-Product2-Product3#2-4-1) where Product1-Product2-Product3 are products and 2-4-1 are quantities. I am struggling to make a table out of it where products and quantities are shown in rows. here is what i tried. list($products, $quantities) = explode("#", $specs); //to separate Quantities from products <?php foreach(preg_split("/-/", $products) as $product_list) { ?> <tr> <td> <?php echo $product_list; ?></td><? } ?> <?php foreach(preg_split("/-/", $quantities) as $qtylist) { ?> <td></td> <td><?php echo $qtylist; ?></td> <?php } ?> </tr> <?php //} ?> </table> I know something is wrong with this and its giving me unexpected reasult Please help me create a table out of the above string. Many Thanks -Max Quote Link to comment https://forums.phpfreaks.com/topic/259376-creating-table-from-string/ Share on other sites More sharing options...
samshel Posted March 20, 2012 Share Posted March 20, 2012 what is the output ur getting? Quote Link to comment https://forums.phpfreaks.com/topic/259376-creating-table-from-string/#findComment-1329661 Share on other sites More sharing options...
litebearer Posted March 20, 2012 Share Posted March 20, 2012 Something like... list($products, $quantities) = explode("#", $specs); //to separate Quantities from products $p_1 = explode("-", $products); $q_1 = explode("-", $quantites); $x = count ($p_1); ?> <table> <?PHP for($i=0;$i<$x;$i++) ( ?> <tr><td><?php echo $p_1[$i]; ?></td><td><?PHP echo $q_1[$i]; ?></td></tr> <?PHP } </table> Quote Link to comment https://forums.phpfreaks.com/topic/259376-creating-table-from-string/#findComment-1329663 Share on other sites More sharing options...
max_w1 Posted March 20, 2012 Author Share Posted March 20, 2012 a badly deformed table, with rows and columns all messed up. Here is the Table code i got from FireBug after compiling in firefox. <table width="200" border="1"> <tbody> <tr> <td>Product Name </td> <td>Quantity</td> <td>Price</td> </tr> <tr> <td> 2600x5</td> <td> NMx001</td> <td></td> <td>2</td> <td></td> <td>9</td> </tr> </tbody> </table> Quote Link to comment https://forums.phpfreaks.com/topic/259376-creating-table-from-string/#findComment-1329665 Share on other sites More sharing options...
litebearer Posted March 20, 2012 Share Posted March 20, 2012 Show the code that produced the bad table Quote Link to comment https://forums.phpfreaks.com/topic/259376-creating-table-from-string/#findComment-1329667 Share on other sites More sharing options...
max_w1 Posted March 20, 2012 Author Share Posted March 20, 2012 Oh great, it works all perfect. thank you so much litebearer. the code you sent had very tiny corrections but it worked like magic. I must say you are example of a brilliant mind. Thank you again. Quote Link to comment https://forums.phpfreaks.com/topic/259376-creating-table-from-string/#findComment-1329668 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.