echo table records

Discussion in 'PHP' started by mero2020, Jan 15, 2014.

  1. #1
    <? 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
     
    mero2020, Jan 15, 2014 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    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.
     
    PoPSiCLe, Jan 15, 2014 IP
  3. mero2020

    mero2020 Greenhorn

    Messages:
    20
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #3
    
    <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:
     
    Last edited: Jan 15, 2014
    mero2020, Jan 15, 2014 IP
  4. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #4
    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.
     
    deathshadow, Jan 16, 2014 IP
  5. Ved pandey

    Ved pandey Greenhorn

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    21
    #5
    <?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>
     
    Ved pandey, Jan 16, 2014 IP