<? foreach($pieces as $piece) {?> <td><? echo $piece;?></td><? }?> PHP: the problem i want to echo $piece at specific <td> like this $pieces(book1,book3,book4) $pieces(book2,book4) $pieces(book1,book3) book1|book2|book3|book4 ------------------------------- book1 -------- book3 book4 -------book2 -------- book4 book1-------- book3 ------- I have many rows
To avoid too much over-complicated code, you need some sort of identifier - you could use an count($pieces) and base it on that, but you still need to check the id/number/unique-something for each entry. could you post some actual code, instead of examples? Ie. some actual $pieces-arrays? What you could do, probably, is split the content of each item and check to see which number it is.
<table> <tr> <td > num </td> <td > book name </td><?php $sqlj=mysql_query("select * from books"); while($row=mysql_fetch_array($sqlj)) {?> <td > <?echo $row[book_branches];?> </td><?}?> </tr> $sql="select * from books where book_branches like'%,%' "; $result=mysql_query($sql); $count=mysql_num_rows($result); echo $count; while($rows=mysql_fetch_array($result)){ //echo $rows['book_num'].' '.$rows['book_name'].' '.$rows['book_branches'].'<br />'; //$branch=explode(",", $branches); ?><tr><td><? echo $rows['book_num'];?> </td> <td width="10%"><? echo $rows['book_name'].' ';?></td> <td><? $branch=$rows['book_branches']; $pieces = explode(",", $branch); foreach($pieces as $piece) {?> <td ><? echo $piece;?></td><? } ?></td> </tr> <?}?> </table> PHP:
Some advice: 1) STOP opening and closing php so damned much. Sloppy illegible code is all you'll get as a result. You in fact have broken code because of it. 2) This is 2014, not 2005. You have NO business using mysql_ functions in new code, hence the GIANT RED WARNING BOXES in the manual? Notice those? 3) some formatting for your output would probably help make it clearer what's going on. Tab key, USE IT! Your HTML doesn't make any sense -- I ALMOST suspect your first TR should be filled with TH, not TD -- but I'd have to see the data to say for certain. Yeah, looking at it I can't make heads nor tails of what you are trying to do -- seeing the data MIGHT help, but since you aren't even plugging in the values for your LIKE statement it really doesn't make any sense.
<?php $peice=array("b1","b2","b3","b4","b5","b6","b7","b8"); ?> <table> <?php for($i=0; $i<count($peice);$i++) { ?> <tr> <?php for($j=0;$j<count($peice);$j++) { if($i&1) { if($j&1) { ?> <td> <?php echo $peice[$j]; ?> </td> <?php } else { ?> <td>.....</td> <?php } } else { if($j&1) { ?> <td>.....</td> <?php } else { ?> <td> <?php echo $peice[$j]; ?></td> <?php } } } ?> </tr> <?php } ?> </table>