displaying a varying number of images.. in tables?!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • guitarromantic@gmail.com

    displaying a varying number of images.. in tables?!

    Hey all.

    I'm building a news system. I have a page to display all posts made to
    category=$id, simple enough, now I wanna make an index page that looks
    up all the categories from the news_topics table, displays their name,
    a link to their page, and the image assigned to that category.

    Here's the catch: I wanna put this in a table as the images are only a
    few hundred pixels wide, so a flat list of each image would be boring
    and inefficient. The thing is, we may add/remove news categories over
    time, so how can I deal with this?

    Would it really be easier to manually make this in HTML and change it
    as/when we make modifications to the topics table?

    -Matt

  • DJ Craig

    #2
    Re: displaying a varying number of images.. in tables?!

    Don't do this manually. You just need to have some code that will take
    all of the images that need to be displayed in an array (say $images is
    an array that contains the HTML code for all of the images) and have a
    loop that goes through $images putting it in a table. It could have a
    variable that keeps track of what column its on, and when it reaches a
    certain number, go to the next row and reset the column counter
    variable. When it reaches the end of $images, it just writes a few
    "<td></td>" 's to fill the rest of that row, then closes the table.

    Comment

    • doffer

      #3
      Re: displaying a varying number of images.. in tables?!

      I assume you've already connected to the mysql server, that you have
      chosen database and that the connection is open... Then this should be
      somehow what like what you need:

      //Do query

      $query = "SELECT * FROM `news_categorie s`";

      $resultat = mysql_query($qu ery);

      //Start loop

      while ($row= @mysql_fetch_ar ray($resultat)) {

      //Fetch info from the row
      $cat_name = $row["cat_name"]; //fetch the name of the category
      $cat_link = $row["cat_link"]; //fetch the link to the category
      $cat_image = $row["cat_image"]; //fetch the image assigned to the
      category

      //Make the HTML

      echo("<tr><td>$ cat_name</td><td><a
      href=\"$cat_lin k\">$cat_link </a></td><td><img src=\"$cat_img\ "
      alt=\"cat_name\ "></td></tr>

      $count++ ;
      }

      I have not tested it, but it should work...

      Comment

      • guitarromantic@gmail.com

        #4
        Re: displaying a varying number of images.. in tables?!

        doffer:

        thanks for the code, but all it does it display a table of images one
        by one in rows.. not what I need. I know theoretically what I need to
        do (count the number of rows returned, and every three rows or so, echo
        a "</td><td>") but I don't know how to actually code it.

        Comment

        • Geoff Berrow

          #5
          Re: displaying a varying number of images.. in tables?!

          I noticed that Message-ID:
          <1122637460.994 585.275610@g47g 2000cwa.googleg roups.com> from
          guitarromantic@ gmail.com contained the following:
          [color=blue]
          >thanks for the code, but all it does it display a table of images one
          >by one in rows.. not what I need. I know theoretically what I need to
          >do (count the number of rows returned, and every three rows or so, echo
          >a "</td><td>") but I don't know how to actually code it.[/color]

          Probably not the most elegant way but...

          $result=mysql_q uery($sql);

          $row[0]="";
          $columns=3;
          $i=0;
          $j=0;
          print "<table>\n" ;
          while($myrow=my sql_fetch_array ($result)){

          if($i==0 || $i%$columns!==0 ){
          $row[$j].=" <td>".$myrow['Make']."</td>\n";
          $i++;
          }
          else{
          $j++;
          $row[$j]="";
          $row[$j].=" <td>".$myrow['Make']."$j</td>\n";
          $i++;
          }
          }
          while($i%$colum ns!==0){
          $row[$j].=" <td>&nbsp;</td>\n";
          $i++;
          }
          foreach($row as $value){
          print " <tr>\n$value </tr>\n";
          }
          print"</table>";

          --
          Geoff Berrow (put thecat out to email)
          It's only Usenet, no one dies.
          My opinions, not the committee's, mine.
          Simple RFDs http://www.ckdog.co.uk/rfdmaker/

          Comment

          • Geoff Berrow

            #6
            Re: displaying a varying number of images.. in tables?!

            I noticed that Message-ID: <r5cke1dmbml3ch 23pfaa3omdqga87 d07n5@4ax.com>
            from Geoff Berrow contained the following:
            [color=blue]
            >Probably not the most elegant way but...
            >[/color]
            Forgot to get rid of the debugging

            $result=mysql_q uery($sql);

            $row[0]="";
            $columns=3;
            $i=0;
            $j=0;
            print "<table>\n" ;
            while($myrow=my sql_fetch_array ($result)){

            if($i==0 || $i%$columns!==0 ){
            $row[$j].=" <td>".$myrow['myfield']."</td>\n";
            $i++;
            }
            else{
            $j++;
            $row[$j]="";
            $row[$j].=" <td>".$myrow['myfield']."</td>\n";
            $i++;
            }
            }
            while($i%$colum ns!==0){
            $row[$j].=" <td>&nbsp;</td>\n";
            $i++;
            }
            foreach($row as $value){
            print " <tr>\n$value </tr>\n";
            }
            print"</table>";
            --
            Geoff Berrow (put thecat out to email)
            It's only Usenet, no one dies.
            My opinions, not the committee's, mine.
            Simple RFDs http://www.ckdog.co.uk/rfdmaker/

            Comment

            • guitarromantic@gmail.com

              #7
              Re: displaying a varying number of images.. in tables?!

              Thanks Geoff! After a few tweaks to display the images that worked
              perfectly:


              I need to credit you somewhere on this site when it's all done for all
              the support, haha..

              Comment

              • Geoff Berrow

                #8
                Re: displaying a varying number of images.. in tables?!

                I noticed that Message-ID:
                <1122647966.354 930.289750@g49g 2000cwa.googleg roups.com> from
                guitarromantic@ gmail.com contained the following:
                [color=blue]
                >Thanks Geoff! After a few tweaks to display the images that worked
                >perfectly:
                >http://www.scenepointblank.com/matt/dev/news/
                >
                >I need to credit you somewhere on this site when it's all done for all
                >the support, haha..[/color]

                That would be nice :)

                --
                Geoff Berrow (put thecat out to email)
                It's only Usenet, no one dies.
                My opinions, not the committee's, mine.
                Simple RFDs http://www.ckdog.co.uk/rfdmaker/

                Comment

                Working...