php generated table lists problems.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Advo

    php generated table lists problems.

    Hi there.

    Im trying to generate a table and its content from a database, and
    display it 5 products across, and as many rows as it needs down.

    Ive kind of done this, but the products are being listed like:

    Name Name Name Name Name Name Name Name Name Name

    Image Image Image Image Image Image Image Image Image Image

    descr descr descr descr descr descr descr descr descr descr descr


    so theyre going across the page for ever and ever, rather than

    Name Name Name Name Name

    Image Image Image Image Image

    Descr Descr Descr Descr Descr

    Name Name Name Name Name

    Image Image Image Image Image

    Descr Descr Descr Descr Descr


    if that makes sense. Any ideas please, im pretty stuck. The code is:

    for ($i = 0;$i< 5; $i++) {
    $j++;

    $name .= '<td style="padding-bottom: 3px;">'.
    $array_category _name[$i] . '</td>';
    $imagename .= '<td>'. '<img src="images/'
    ..$array_catego ry_image_name[$i] . '" width="74" height="59">' .
    '</td>';
    $description .= '<td>'. $array_category _description[$i] . '</td>';

    }
    echo '<table width="100%" border="0" align="center" cellpadding="0"
    cellspacing="0" >';
    echo "<tr align=\"center\ ">$name</tr>";
    echo "<br>";
    echo "<tr align=\"center\ ">$imagenam e</tr>";
    echo "<tr align=\"center\ ">$descript ion</tr>";
    echo "<tr align=\"center\ "><td collspan='3'&nb sp;</td></tr>";
    echo "</table>";

  • SBGamesCone

    #2
    Re: php generated table lists problems.



    On Dec 13, 11:02 am, "Advo" <max_misch...@h otmail.comwrote :
    Hi there.
    >
    Im trying to generate a table and its content from a database, and
    display it 5 products across, and as many rows as it needs down.
    >
    Ive kind of done this, but the products are being listed like:
    >
    Name Name Name Name Name Name Name Name Name Name
    >
    Image Image Image Image Image Image Image Image Image Image
    >
    descr descr descr descr descr descr descr descr descr descr descr
    >
    so theyre going across the page for ever and ever, rather than
    >
    Name Name Name Name Name
    >
    Image Image Image Image Image
    >
    Descr Descr Descr Descr Descr
    >
    Name Name Name Name Name
    >
    Image Image Image Image Image
    >
    Descr Descr Descr Descr Descr
    >
    if that makes sense. Any ideas please, im pretty stuck. The code is:
    >
    for ($i = 0;$i< 5; $i++) {
    $j++;
    >
    $name .= '<td style="padding-bottom: 3px;">'.
    $array_category _name[$i] . '</td>';
    $imagename .= '<td>'. '<img src="images/'
    .$array_categor y_image_name[$i] . '" width="74" height="59">' .
    '</td>';
    $description .= '<td>'. $array_category _description[$i] . '</td>';
    >
    }
    echo '<table width="100%" border="0" align="center" cellpadding="0"
    cellspacing="0" >';
    echo "<tr align=\"center\ ">$name</tr>";
    echo "<br>";
    echo "<tr align=\"center\ ">$imagenam e</tr>";
    echo "<tr align=\"center\ ">$descript ion</tr>";
    echo "<tr align=\"center\ "><td collspan='3' </td></tr>";
    echo "</table>";
    I cleaned up the code a little bit, but I would first initialize the
    variables before the for loop. You are adding data to them without
    ensuring they are empty first. Try this:

    unset($name, $imagename, $description);
    for ($i = 0;$i< 5; $i++) {
    $j++;
    $name .= '<td style="padding-bottom:
    3px;">'.$array_ category_name[$i].'</td>';
    $imagename .= '<td><img
    src="images/'.$array_catego ry_image_name[$i] . '" width="74"
    height="59"></td>';
    $description .= '<td>'. $array_category _description[$i] . '</td>';
    }
    echo '<table width="100%" border="0" align="center" cellpadding="0"
    cellspacing="0" >';
    echo "<tr align=\"center\ ">$name</tr>";
    echo "<br>";
    echo "<tr align=\"center\ ">$imagenam e</tr>";
    echo "<tr align=\"center\ ">$descript ion</tr>";
    echo "<tr align=\"center\ "><td collspan='3' </td></tr>";
    echo "</table>";

    Also, what is the point of $j?

    Comment

    • Advo

      #3
      Re: php generated table lists problems.


      SBGamesCone wrote:
      On Dec 13, 11:02 am, "Advo" <max_misch...@h otmail.comwrote :
      Hi there.

      Im trying to generate a table and its content from a database, and
      display it 5 products across, and as many rows as it needs down.

      Ive kind of done this, but the products are being listed like:

      Name Name Name Name Name Name Name Name Name Name

      Image Image Image Image Image Image Image Image Image Image

      descr descr descr descr descr descr descr descr descr descr descr

      so theyre going across the page for ever and ever, rather than

      Name Name Name Name Name

      Image Image Image Image Image

      Descr Descr Descr Descr Descr

      Name Name Name Name Name

      Image Image Image Image Image

      Descr Descr Descr Descr Descr

      if that makes sense. Any ideas please, im pretty stuck. The code is:

      for ($i = 0;$i< 5; $i++) {
      $j++;

      $name .= '<td style="padding-bottom: 3px;">'.
      $array_category _name[$i] . '</td>';
      $imagename .= '<td>'. '<img src="images/'
      .$array_categor y_image_name[$i] . '" width="74" height="59">' .
      '</td>';
      $description .= '<td>'. $array_category _description[$i] . '</td>';

      }
      echo '<table width="100%" border="0" align="center" cellpadding="0"
      cellspacing="0" >';
      echo "<tr align=\"center\ ">$name</tr>";
      echo "<br>";
      echo "<tr align=\"center\ ">$imagenam e</tr>";
      echo "<tr align=\"center\ ">$descript ion</tr>";
      echo "<tr align=\"center\ "><td collspan='3' </td></tr>";
      echo "</table>";
      >
      I cleaned up the code a little bit, but I would first initialize the
      variables before the for loop. You are adding data to them without
      ensuring they are empty first. Try this:
      >
      unset($name, $imagename, $description);
      for ($i = 0;$i< 5; $i++) {
      $j++;
      $name .= '<td style="padding-bottom:
      3px;">'.$array_ category_name[$i].'</td>';
      $imagename .= '<td><img
      src="images/'.$array_catego ry_image_name[$i] . '" width="74"
      height="59"></td>';
      $description .= '<td>'. $array_category _description[$i] . '</td>';
      }
      echo '<table width="100%" border="0" align="center" cellpadding="0"
      cellspacing="0" >';
      echo "<tr align=\"center\ ">$name</tr>";
      echo "<br>";
      echo "<tr align=\"center\ ">$imagenam e</tr>";
      echo "<tr align=\"center\ ">$descript ion</tr>";
      echo "<tr align=\"center\ "><td collspan='3' </td></tr>";
      echo "</table>";
      >
      Also, what is the point of $j?
      hey, thanks for the prompt reply. $j is nothing, i was testing before
      hand.
      In the for loop i changed from:

      for ($i = 0;$i< 5; $i++) {

      to

      for ($i = 0;$i< $number_of_tota l_rows; $i++) {

      the code works, but the products still go across the page for eternity,
      rather than cutting off after 5 products, then going to the next row if
      that makes any sense.

      any ideas?

      Comment

      • Jerry Stuckle

        #4
        Re: php generated table lists problems.

        Advo wrote:
        Hi there.
        >
        Im trying to generate a table and its content from a database, and
        display it 5 products across, and as many rows as it needs down.
        >
        Ive kind of done this, but the products are being listed like:
        >
        Name Name Name Name Name Name Name Name Name Name
        >
        Image Image Image Image Image Image Image Image Image Image
        >
        descr descr descr descr descr descr descr descr descr descr descr
        >
        >
        so theyre going across the page for ever and ever, rather than
        >
        Name Name Name Name Name
        >
        Image Image Image Image Image
        >
        Descr Descr Descr Descr Descr
        >
        Name Name Name Name Name
        >
        Image Image Image Image Image
        >
        Descr Descr Descr Descr Descr
        >
        >
        if that makes sense. Any ideas please, im pretty stuck. The code is:
        >
        for ($i = 0;$i< 5; $i++) {
        $j++;
        >
        $name .= '<td style="padding-bottom: 3px;">'.
        $array_category _name[$i] . '</td>';
        $imagename .= '<td>'. '<img src="images/'
        .$array_categor y_image_name[$i] . '" width="74" height="59">' .
        '</td>';
        $description .= '<td>'. $array_category _description[$i] . '</td>';
        >
        }
        echo '<table width="100%" border="0" align="center" cellpadding="0"
        cellspacing="0" >';
        echo "<tr align=\"center\ ">$name</tr>";
        echo "<br>";
        echo "<tr align=\"center\ ">$imagenam e</tr>";
        echo "<tr align=\"center\ ">$descript ion</tr>";
        echo "<tr align=\"center\ "><td collspan='3'&nb sp;</td></tr>";
        echo "</table>";
        >

        First of all, do you want one table with many rows, or many tables?
        Your code (if it worked properly) would create many tables (the <table
        statement is within your loop).

        Assuming you want one table with multiple rows, there are several ways
        to do it. The easiest is probably to just add a new row every time you
        get another 5 items (warning - not tried, may contain syntax errors!):


        echo '<table width="100%" border="0" align="center" ' .
        'cellpadding="0 "' cellspacing="0" >';
        $name = $imagename = $description = '';

        // Assuming all arrays have the same # of elements
        for ($i = 0;$i count($array_ca tegory_name); $i++) {
        if (($i % 5) && ($name <'')) { // Every 5th line except first
        echo "<tr align=\"center\ ">$name</tr>\n";
        echo "<tr align=\"center\ ">$imagenam e</tr>\n";
        echo "<tr align=\"center\ ">$descript ion</tr>\n";
        $name = $imagename = $description = '';
        }
        $name .= '<td style="padding-bottom: 3px;">'.
        $array_category _name[$i] . '</td>';
        $imagename .= '<td>'.
        '<img src="images/'.$array_catego ry_image_name[$i] .
        '" width="74" height="59">' . '</td>';
        $description .= '<td>'. $array_category _description[$i] .
        '</td>';
        }

        // Might have a partial last rows) - complete them
        for ($i = count($array_ca tegory_names); $i % 5 0; $i++)
        $name .= "<td>&nbsp; </td>\n";
        $imagename .= "<td>&nbsp; </td>\n";
        $description .= "<td>&nbsp; </td>\n";
        }
        if ($name <'') { // If something left in it
        echo "<tr align=\"center\ ">$name</tr>\n";
        echo "<tr align=\"center\ ">$imagenam e</tr>\n";
        echo "<tr align=\"center\ ">$descript ion</tr>\n";
        }
        echo "</table>";

        --
        =============== ===
        Remove the "x" from my email address
        Jerry Stuckle
        JDS Computer Training Corp.
        jstucklex@attgl obal.net
        =============== ===

        Comment

        Working...