Jump to content

how do i loop within a loop?


jarv

Recommended Posts

I want to loop $result2? at the moment $Keyword only shows 1 keyword whereas there are several for each HeaderID

<?php
include_once("config.php");
include_once("functions.php");
// Check user logged in already:
// checkLoggedIn("yes");
doCSS(); ?>
<div id="header">
<p class="wrap">

<? print("\n <a href=\"post.php"."\">+ Add new header</a> ");?>
</p>
<h1 class="wrap">Avert Weekly Headers</h1>
</div>
<?
include_once("config.php");
$result = mysql_query("SELECT * FROM Headertbl ORDER BY HeaderDate DESC");


while($row = mysql_fetch_array($result)){


echo '<div id="entry" class="wrap">';
$HeaderID = $row['HeaderID'];
$HeaderImage = $row['HeaderImage'];
$HeaderDate = $row['HeaderDate'];
$HeaderPhotoNo = $row['HeaderPhotoNo'];
$HeaderName = $row['HeaderName'];
$PhotoID = $row['PhotoID'];
$CountryID = $row['CountryID'];

$result1 = mysql_fetch_array(mysql_query("SELECT * FROM country WHERE CountryID =".$CountryID));
$Country = $result1['country'];

$result2 = mysql_query("SELECT * FROM keyword INNER JOIN keywordlist ON keyword.keyword_id=keywordlist.keyword_id WHERE keyword.HeaderID=".$HeaderID);

while($row1 = mysql_fetch_array($result2))
  $Keyword = $row1['keyword'];


echo <<<EOF

  <div class="entry_header">
    <b>Header Name:</b> $HeaderName <a href="edit.php?HeaderID=$HeaderID">Edit Header</a> | <a href="delete.php?HeaderID=$HeaderID">Delete Header</a><br />
<a href="upload.php?HeaderID=$HeaderID">Change Header</a><br />
<a href="http://www.avert.org/photo_search.php?search_keyword_id=&search_country_id=country-$CountryID&page_type=thumbnails&search=search">Other pictures from $Country</a> Keywords:{$Keyword}
  <br />
<b>PhotoID:</b> $PhotoID <br />
<b>Header Photo No:</b> $HeaderPhotoNo <br />
<b>Header Date:</b> $HeaderDate <br />
<img src="headers/$HeaderImage">
  </div>
EOF;
echo '</div>';
}
mysql_close($link);
?>

Link to comment
https://forums.phpfreaks.com/topic/112179-how-do-i-loop-within-a-loop/
Share on other sites

Jarv,

 

It looks like to me you are missing the '{' and '} for you second while loop.

 

The way you have it coded :

 

while($row1 = mysql_fetch_array($result2))

  $Keyword = $row1['keyword'];

 

only the last $Keyword will be passed to your html out..

 

 

Try :

 

while($row1 = mysql_fetch_array($result2)) {

  $Keyword = $row1['keyword'];

 

Then :

 

}

 

at the bottom of your script.

 

Scot L. Diddle, Richmond VA

 

Yes, ScotDiddle is right, you need curly braces for code blocks. If it's just a single line of code then you don't need the braces.

 

e.g.

 

$name = 'Wolphie';
if($name == 'Wolphie')
  print 'My name is ' . $name;


// For braces

$name = 'Wolphie';
$age = 19;
if(($name == 'Wolphie') && ($age == 19)) {
  print 'My name is ' . $name;
  print '<br />';
  print 'My age is ' . $age;
}
else
  print 'Your name is not ' . $name;

thanks, I tried what you said, if i put the } right at the ottom with the other one, it seems to work but messes up my CSS and loops more headers from the first loop so i put the } after $keyword and now i get: Parse error: syntax error, unexpected $end in /Users/staff/Sites/johns/headers/display.php on line 59

 

here is my code so far:

<?php
include_once("config.php");
include_once("functions.php");
// Check user logged in already:
// checkLoggedIn("yes");
doCSS(); ?>
<div id="header">
<p class="wrap">

<? print("\n <a href=\"post.php"."\">+ Add new header</a> ");?>
</p>
<h1 class="wrap">Avert Weekly Headers</h1>
</div>
<?
include_once("config.php");
$result = mysql_query("SELECT * FROM Headertbl ORDER BY HeaderDate DESC");


while($row = mysql_fetch_array($result)){


echo '<div id="entry" class="wrap">';
$HeaderID = $row['HeaderID'];
$HeaderImage = $row['HeaderImage'];
$HeaderDate = $row['HeaderDate'];
$HeaderPhotoNo = $row['HeaderPhotoNo'];
$HeaderName = $row['HeaderName'];
$PhotoID = $row['PhotoID'];
$CountryID = $row['CountryID'];

$result1 = mysql_fetch_array(mysql_query("SELECT * FROM country WHERE CountryID =".$CountryID));
$Country = $result1['country'];

$result2 = mysql_query("SELECT * FROM keyword INNER JOIN keywordlist ON keyword.keyword_id=keywordlist.keyword_id WHERE keyword.HeaderID=".$HeaderID);

while($row1 = mysql_fetch_array($result2)){
$Keyword = $row1['keyword'];


echo <<<EOF

  <div class="entry_header">
    <b>Header Name:</b> $HeaderName <a href="edit.php?HeaderID=$HeaderID">Edit Header</a> | <a href="delete.php?HeaderID=$HeaderID">Delete Header</a><br />
<a href="upload.php?HeaderID=$HeaderID">Change Header</a><br />
<a href="http://www.avert.org/photo_search.php?search_keyword_id=&search_country_id=country-$CountryID&page_type=thumbnails&search=search">Other pictures from $Country</a> Keywords:$Keyword}
<br />
<b>PhotoID:</b> $PhotoID <br />
<b>Header Photo No:</b> $HeaderPhotoNo <br />
<b>Header Date:</b> $HeaderDate <br />
<img src="headers/$HeaderImage">
  </div>

EOF;
echo '</div>';

}
mysql_close($link);

?>

Jarv,

 

What happens if your move your second sql query to the top, like the following ?

 

Scot

 


<?php

include_once("config.php");
include_once("functions.php");
// Check user logged in already:
// checkLoggedIn("yes");
doCSS(); 

?>

<div id="header">
<p class="wrap">

<? print("\n <a href=\"post.php"."\">+ Add new header</a> ");?>
</p>
<h1 class="wrap">Avert Weekly Headers</h1>
</div>

<?php

include_once("config.php");

$result = mysql_query("SELECT * FROM Headertbl ORDER BY HeaderDate DESC");

$result2 = mysql_query("SELECT * FROM keyword INNER JOIN keywordlist ON keyword.keyword_id=keywordlist.keyword_id WHERE keyword.HeaderID=".$HeaderID);

while($row = mysql_fetch_array($result)){

	echo '<div id="entry" class="wrap">';

	$HeaderID = $row['HeaderID'];
	$HeaderImage = $row['HeaderImage'];
	$HeaderDate = $row['HeaderDate'];
	$HeaderPhotoNo = $row['HeaderPhotoNo'];
	$HeaderName = $row['HeaderName'];
	$PhotoID = $row['PhotoID'];
	$CountryID = $row['CountryID'];

	$result1 = mysql_fetch_array(mysql_query("SELECT * FROM country WHERE CountryID =".$CountryID));

	$Country = $result1['country'];

	while($row1 = mysql_fetch_array($result2)) {

		$Keyword = $row1['keyword'];


		echo <<<EOF

		  <div class="entry_header">
		    <b>Header Name:</b> $HeaderName <a href="edit.php?HeaderID=$HeaderID">Edit Header</a> | <a href="delete.php?HeaderID=$HeaderID">Delete Header</a><br />
			<a href="upload.php?HeaderID=$HeaderID">Change Header</a><br />
			<a href="http://www.avert.org/photo_search.php?search_keyword_id=&search_country_id=country-$CountryID&page_type=thumbnails&search=search">Other pictures from $Country</a> Keywords:$Keyword}
			<br />
			<b>PhotoID:</b> $PhotoID <br />
			<b>Header Photo No:</b> $HeaderPhotoNo <br />
			<b>Header Date:</b> $HeaderDate <br />
			<img src="headers/$HeaderImage">
		  </div>

EOF;
		echo '</div>';

	}

}

mysql_close($link);

?>

I get several errors:

 

Notice: Undefined variable: HeaderID in /Users/staff/Sites/johns/headers/display.php on line 26

 

 

and looped errors:

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /Users/staff/Sites/johns/headers/display.php on line 44

 

 

The problem with the OP's code is that nothing was done with the $keyword in the loop.

 

Here's one way of solving the problem without doing much changes to the script flow:

<?php
include_once("config.php");
include_once("functions.php");
// Check user logged in already:
// checkLoggedIn("yes");
doCSS(); ?>
<div id="header">
<p class="wrap">

<? print("\n <a href=\"post.php"."\">+ Add new header</a> ");?>
</p>
<h1 class="wrap">Avert Weekly Headers</h1>
</div>
<?
include_once("config.php");
$result = mysql_query("SELECT * FROM Headertbl ORDER BY HeaderDate DESC");


while($row = mysql_fetch_array($result)){


echo '<div id="entry" class="wrap">';
$HeaderID = $row['HeaderID'];
$HeaderImage = $row['HeaderImage'];
$HeaderDate = $row['HeaderDate'];
$HeaderPhotoNo = $row['HeaderPhotoNo'];
$HeaderName = $row['HeaderName'];
$PhotoID = $row['PhotoID'];
$CountryID = $row['CountryID'];

$result1 = mysql_fetch_array(mysql_query("SELECT * FROM country WHERE CountryID =".$CountryID));
$Country = $result1['country'];

$result2 = mysql_query("SELECT * FROM keyword INNER JOIN keywordlist ON keyword.keyword_id=keywordlist.keyword_id WHERE keyword.HeaderID=".$HeaderID);

$keywords = array();
while($row1 = mysql_fetch_array($result2))
  $keywords[] = $row1['keyword'];

$tmp = array();
$tmp[] = '<div class="entry_header">';
$tmp[] = '<b>Header Name:</b> $HeaderName <a href="edit.php?HeaderID=' . $HeaderID . '">Edit Header</a> | <a href="delete.php?HeaderID=' . $HeaderID . '">Delete Header</a><br />';
$tmp[] = '<a href="upload.php?HeaderID=' . $HeaderID .'">Change Header</a><br />';
$tmp[] = '<a href="http://www.avert.org/photo_search.php?search_keyword_id=&search_country_id=country-$CountryID&page_type=thumbnails&search=search">Other pictures from ' . $Country . '</a> Keywords: ' . implode(', ',$keywords) . '<br />';
$tmp[] = '<b>PhotoID:</b> ' . $PhotoID . '<br />';
$tmp[] = '<b>Header Photo No:</b> ' . $HeaderPhotoNo . '<br />';
$tmp[] = '<b>Header Date:</b> ' . $HeaderDate  . '<br />';
$tmp[] = '<img src="headers/' . $HeaderImage . '">.';
$tmp[] = '</div>';
$tmp[] = '</div>';
echo implode("\n",$tmp);
}
mysql_close($link);
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.