By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,441 Members | 1,468 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,441 IT Pros & Developers. It's quick & easy.

while echo output

P: n/a
I have now spent well over 12 hours on this one problem.

I am pulling data out of a database, the data in the database is categorized by a value (season).
I need to title the first value, then echo the listings that would fall into that category. Once this is completed I need to then label the next value and echo the values that would fall into that category... so on and so forth..

this is concept I have had so far:

include("./include/db.inc.php");
$sql = "SELECT * FROM table WHERE view=1 ORDER BY file";
$result = mysql_query($sql, $dbconn) or die(mysql_error());

while ($newArray = mysql_fetch_array($result)) {
$id = $newArray['id'];
$season = $newArray['season'];
$file = $newArray['file'];
$name = $newArray['name'];
$history = $newArray['history'];
$view = $newArray['view'];

if ($season == "0") {
echo "<b>--- Pre-Season ---</b><br/>";
echo "<a href=\"images/$file.jpg\">$name</a><br/>\n";
}

if ($season == "1") {
echo "<b>--- 1st Season ---</b><br/>";
echo "<a href=\"images/$file.jpg\">$name</a><br/>\n";
}

if ($season == "2") {
echo "<b>--- 2nd Season ---</b><br/>";
echo "<a href=\"images/$file.jpg\">$name</a><br/>\n";
}
}

?>

The output I get from this is:

<b>--- Pre-Season ---</b><br/>
<a href=\"images/value1-file.jpg\">value1-name</a><br/>

<b>--- Pre-Season ---</b><br/>
<a href=\"images/value2-file.jpg\">value2-name</a><br/>

<b>--- Pre-Season ---</b><br/>
<a href=\"images/value3-file.jpg\">value3-name</a><br/>

<b>--- 1st Season ---</b><br/>
<a href=\"images/value4-file.jpg\">value4-name</a><br/>

<b>--- 1st Season ---</b><br/>
<a href=\"images/value5-file.jpg\">value5-name</a><br/>

<b>--- 2nd Season ---</b><br/>
<a href=\"images/value6-file.jpg\">value6-name</a><br/>

<b>--- 2nd Season ---</b><br/>
<a href=\"images/value7-file.jpg\">value7-name</a><br/>
What I was expecting was:

<b>--- Pre-Season ---</b><br/>
<a href=\"images/value1-file.jpg\">value1-name</a><br/>
<a href=\"images/value2-file.jpg\">value2-name</a><br/>
<a href=\"images/value3-file.jpg\">value3-name</a><br/>

<b>--- 1st Season ---</b><br/>
<a href=\"images/value4-file.jpg\">value4-name</a><br/>
<a href=\"images/value5-file.jpg\">value5-name</a><br/>

<b>--- 2nd Season ---</b><br/>
<a href=\"images/value6-file.jpg\">value6-name</a><br/>
<a href=\"images/value7-file.jpg\">value7-name</a><br/>
Thank you for all your help in advance...
(sorry if this doesn't make sense - the past 12 straight hours have been trying to figure this out)
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

the only problem you have to solve, ist t oremember the last value of the
variable $session.

Just try this (untested):

$lastseason = ""; // remember last value of season

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

$id = $newArray['id'];
$season = $newArray['season'];
$file = $newArray['file'];
$name = $newArray['name'];
$history = $newArray['history'];
$view = $newArray['view'];

if($season != $lastseason){

switch($season){ // better than millions of ifs
case "0":
print "<b>--- Pre-Season ---</b><br/>";
break;
case "1";
print "<b>--- 1st Season ---</b><br/>";
break;
case "2";
print "<b>--- 2nd Season ---</b><br/>";
break;
case "3";
print "<b>--- 3rd Season ---</b><br/>";
break;
default:
print "<b>--- " . $season . "th Season ---</b><br/>";
break;
}

}
echo "<a href=\"images/$file.jpg\">$name</a><br/>\n";
$lastseason = $season;
}

hope this works as you expect!

Greetings
Jul 17 '05 #2

P: n/a
I noticed that Message-ID: <Znr6d.36684$aW5.23906@fed1read07> from Adam
Carolla contained the following:
Thank you for all your help in advance...
(sorry if this doesn't make sense - the past 12 straight hours have been trying to figure this out)


I think I'd do it like this

Run a query to determine the number of distinct seasons

This should give you an array of seasons to work with.

For each season run a separate query and list the results

There are probably more efficient ways, but that would work.

--
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/
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.