Page 1 of 2 12 LastLast
Results 1 to 10 of 15

Thread: How to use foreach to display data from multiple column?

  1. #1
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to use foreach to display data from multiple column?

    Hi..guys! I have a form and use foreach to output value from multiple column in database.What wrong went in the following script?I can only get the movie name but not category as well.How about if category likely to be bracketed?


    PHP Code:
    <tr> 
    <td> 
    <? 
    if (isset($_SESSION['gmemberid'])) { 
        
    $tbl_name "movie"

        
    $result mysql_query("SELECT name,category FROM $tbl_name"
                             or die(
    "Cannot execute query."); 

        
    $numrow mysql_num_rows($result); 

        if (
    $numrow != 0) { 
            for (
    $counter 0$row mysql_fetch_row($result); $counter++) { 
                foreach (
    $row as $key => $value) { 

                    if (
    $key == 0) { 
    ?>                     
                            <strong><br> 
                             <?php echo $value;?></strong></font></td> 
                          </tr> 
    <? 
                 

               } 
             } 
           } 
        } 
    ?>
    Last edited by devil_vin; 09-20-2007 at 08:37 AM.

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    Quote Originally Posted by devil_vin View Post
    PHP Code:
                    if ($key == 0) { 
    ?>                     
                            <strong><br> 
                             <?php echo $value;?></strong></font></td> 
                          </tr> 
    <? 
                 
    }
    this wont print out anything but the first row. you dont want to check if the key is 0. and you are also only printing the value, not the key. to print both you could use somethign like

    PHP Code:
    if() {?>
    <tr>
         <th><?php echo $key ?></th>
         <td><?php echo $value ?></td>
    </tr>
    <?php ?>

  3. #3
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I am using mysql_fetch_array() rather than mysql_fetch_rows().

    PHP Code:
    <?
    if (isset($_SESSION['gmemberid'])) {
        
    $tbl_name "movie";

        
    $result mysql_query("SELECT name,category FROM $tbl_name")
                                or die(
    "Cannot execute query.");

        
    $numrow mysql_num_rows($result);

        if (
    $numrow != 0) {
            while(
    $rows mysql_fetch_array($result)){
                 echo 
    '<strong><br>' $rows[0] .' (' $rows[1] .') </strong>';
            }
               
    ?>
    Let said now I have seven rows of record need to be fetched out,how can I write a single SQL statement to query them out and display properly,I have an auto-increment id column in the table.Thanks...

  4. #4
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    $row[0] tells you very little as to what the key is for this item.
    mysql_fetch_array() allows you to reference by key, rather than row.

    Code:
    if ($numrow != 0) { 
    while($rows = mysql_fetch_array($result)){?>
        <tr>
             <th><?php echo $rows[$key]; ?></th>
             <td><?php echo $rows[$value]; ?></td>
        </tr><?php
            }
    now I have seven rows of record need to be fetched out
    that is what the WHERE clause is for
    Code:
    $result = mysql_query("SELECT name,category FROM $tbl_name WHERE _____")
                                or die("Cannot execute query.");

  5. #5
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Is there any problem with using index.$row[0] is name;$row[1] is category.
    I have a query statement to display all seven recordsbut why the first record is skipped?

    PHP Code:
     $result mysql_query("SELECT name,category FROM $tbl_name
                               WHERE id BETWEEN '1' AND '7'"
    )
                                     or die(
    "Cannot execute query."); 

  6. #6
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    WHERE id<8

  7. #7
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    You can't always guarantee that the first seven IDs will be below 8; for example, if a row is deleted the eighth row will have an ID of 8. Instead, use a LIMIT clause:
    Code:
    $result = mysql_query(sprintf('SELECT name, category FROM %s LIMIT 8', $tbl_name))
      or die('Cannot execute query.');
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  8. #8
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    ok.by this moment how can I align all screening time in a single row?

    PHP Code:
      <table width="100%" height="47" border="0">
                          <tr> 
                            <td height="21" colspan="6"><font face="Arial">------------------------------------------------------------------------------------------------------------------------------ <br>    
                
    <?
    if (isset($_SESSION['gmemberid'])) {
        
    $tbl_name "movie";

       
    $result mysql_query(sprintf('SELECT name,category,screeningTime FROM %s
                 LIMIT 1'
    $tbl_name)) or die('Cannot execute query.');

        
    //$numrow = mysql_num_rows($result);

       
            
    while ($rows mysql_fetch_assoc($result)) {
                echo 
    '<strong>' $rows['name'] . ' (' $rows['category'] . ')
                     <br></strong></font>'
    ;
                foreach (
    explode(','$rows['screeningTime']) as $time) { ?>
                <label>
                <td><input type="radio" value="<?php echo $time?>">
                <?php echo $time?></td>
                </label>
                </td></tr>
                <?php ?>
    <?
            
    }

        
    }
    ?>

  9. #9
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Remove the wrapping <tr> element.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  10. #10
    Join Date
    Aug 2007
    Location
    Malaysia
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for reply.Now the entire thing,including the dashed line is on center,how can keep it on left?

    PHP Code:

                    <table width="100%" border="0">
                    <tr> 
                      <td height="68"> 
                        <table width="100%" height="47" border="0">
                          
                           <font face="Arial">------------------------------------------------------------------------------------------------------------------------------ <br>    
                
    <?
    if (isset($_SESSION['gmemberid'])) {
        
    $tbl_name "movie";

       
    $result mysql_query(sprintf('SELECT name,category,screeningTime FROM %s
                 LIMIT 1'
    $tbl_name)) or die('Cannot execute query.');

        
    //$numrow = mysql_num_rows($result);

       
            
    while ($rows mysql_fetch_assoc($result)) {
                echo 
    '<strong>' $rows['name'] . ' (' $rows['category'] . ')
                     <br></strong></font>'
    ;
                foreach (
    explode(','$rows['screeningTime']) as $time) { ?>
                <label>
                <input type="radio" value="<?php echo $time?>">
                <?php echo $time?>
                </label>
                
                <?php ?>
    <?
            
    }

        
    }
    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •