SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: foreach??

  1. #1
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Newark, Notts, UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach??

    OK, Hello to all of you,
    I am a little stuck so need a little help..

    this is the code in the controller:

    PHP Code:
    <?php

    if (isset($_GET['addinvoice']))
    {
        include 
    'form.html.php';
        exit();
    }

    include 
    '../includes/db.inc.php';

    if (isset(
    $_POST['term_1']))
    {
        global 
    $link;
        
        
    $result mysqli_query($link'SELECT * FROM jos_reg');
        if (!
    $result)
        {
            
    $error 'Error fetching children: ' mysqli_error($link);
            include 
    'error.html.php';
            exit();
        }
        
        while (
    $row mysqli_fetch_array($result))
        {
            
    $id[] = $row['id'];
            
    $familyid[] = $row['familyid'];
        }
        
        include 
    'autumnterm.php';
    }

    ?>
    this is the file autumntern.php;

    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>List of Children</title>
    </head>
    <body>
    	<p>This is for the Autum Term:</p>
    	<?php foreach ($id as $id);?>
    		<blockquote><p>
    		<?php echo htmlspecialchars($id, ENT_QUOTES, 'UTF-8');?>
    </p></blockquote>
    </body>
    </html>

    now I feel that it should be showing all of the id's in the DB, but it is only showing the last one, can someone please tell me what I am doing wrong and tell me what I need to put into the code for it to bring all the id's and also would like to add a few more columns to the foreach, how would I do that, so I would like familid, id and a few more to show in the html include.

    Thanks
    Last edited by Mal Curtis; Aug 13, 2009 at 17:36. Reason: Added code tags

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    .. as before till here ...

    $rows mysqli_fetch_array($result))

    include 
    'autumnterm.php';

    }
    ?> 
    template:
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>List of Children</title>
    </head>
    <body>
    <p>This is for the Autum Term</p>
    <?php
    foreach ($rows as $row) {

      echo 
    '<blockquote><p>' 
      echo 
    htmlspecialchars($row['id'], ENT_QUOTES'UTF-8') ; 
      echo 
    htmlspecialchars($row['familyid'], ENT_QUOTES'UTF-8') ;
      echo 
    '</p></blockquote>' ;

    }
    ?>

    </body>
    </html>
    I'm remiss in not knowing the alternative-php-syntax off by heart, but you should get the idea.

  3. #3
    SitePoint Addict Mal Curtis's Avatar
    Join Date
    Jul 2009
    Location
    New Zealand
    Posts
    327
    Mentioned
    14 Post(s)
    Tagged
    0 Thread(s)
    Code HTML4Strict:
    	<p>This is for the Autum Term:</p>
    	<?php foreach ($id as $id);?>
    		<blockquote><p>
    		<?php echo htmlspecialchars($id, ENT_QUOTES, 'UTF-8');?>
    </p></blockquote>
    </body>
    </html>

    Your foreach is not actually doing anything. You need to define a start and end of the loop (at the moment it's moving to the next statement for a single line, which is instantly finished by the errant semi colon).


    Code HTML4Strict:
    	<p>This is for the Autum Term:</p>
    	<?php foreach ($id as $id){?>
    		<blockquote><p>
    		<?php echo htmlspecialchars($id, ENT_QUOTES, 'UTF-8');?>
    </p></blockquote>
            <?php } ?>
    </body>
    </html>

  4. #4
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Newark, Notts, UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Guys,

    Thank you for your replies, I have tried both of your solutions and I has not changed anything, it still only displays the last ID number and this is using your solution cups as well, with the option of the family ID. I have got most of the code from the build your own database driven web site, and when I do the examples this does the same thing, will ponly bring the last line from the db. If you could think of anything else that would be great.

    Thanks

  5. #5
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    There is no need to over complicate things, do the loop on the autumn include:

    Code PHP:
    if (isset($_GET['addinvoice']))
    {
        include 'form.html.php';
        exit();
    }
     
     
    include '../includes/db.inc.php';
     
    if (isset($_POST['term_1'])) {
     
        global $link;
     
        $result = mysqli_query($link, 'SELECT * FROM jos_reg');
     
        if (!$result)
        {
            $error = 'Error fetching children: ' . mysqli_error($link);
            include 'error.html.php';
            exit();
        }
     
       include 'autumnterm.php';
     
    }


    Code HTML4Strict:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>List of Children</title>
    </head>
    <body>
        <p>This is for the Autum Term:</p>
        <?php
        	while($rows = mysqli_fetch_assoc($result)) {
        		echo "<blockquote><p>".	$rows['id'] .' - ' . $rows['familyid'] ."</p></blockquote>";
    		}
    	?>
     
    </body>
    </html>


    Try that
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  6. #6
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Newark, Notts, UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I must be doing something very wrong somewhere as I have chnaged it to what you have advised spikeZ and this is the error that I am getting:

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/sites/ceskonline.com/public_html/reports/invoice/autumnterm.php on line 13

    Line 13 is the while statement in the autumn include,

  7. #7
    SitePoint Enthusiast
    Join Date
    Jul 2009
    Location
    Newark, Notts, UK
    Posts
    59
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello Guys,

    Just wanted to thank you all for your help, I have managed to fix it. It looks like ZEND studio was being awkward and not updating though FTP all the time so causing a chnage in the code not to work, But I have managed to get it working by using the below if anyone is interested;

    while ($row = mysqli_fetch_array($result))
    {
    $child[] = array('id' => $row['id'], 'familyid' => $row['familyid']);
    }

    include 'autumnterm.php';

    and the include;

    <?php foreach ($child as $childs): ?>
    <blockquote><p>
    <?php echo htmlspecilachars ($childs['id], ENT_QUOTES, 'UTF-8);?>

    etc etc....

    But thank you very much for all the pointers..


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
  •