SitePoint Sponsor |
|
User Tag List
Results 1 to 7 of 7
Thread: foreach??
-
Aug 13, 2009, 13:08 #1
- 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';
}
?>
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.
ThanksLast edited by Mal Curtis; Aug 13, 2009 at 17:36. Reason: Added code tags
-
Aug 13, 2009, 17:38 #2
- 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';
}
?>
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>
-
Aug 13, 2009, 17:42 #3
- 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>
-
Aug 14, 2009, 02:19 #4
- 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
-
Aug 14, 2009, 02:35 #5
- 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 thatMike Swiffin - Community Team Advisor
Only a woman can read between the lines of a one word answer.....
-
Aug 14, 2009, 03:39 #6
- 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,
-
Aug 14, 2009, 06:02 #7
- 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