Array help; for()/foreach() help...
Moderator: General Moderators
Array help; for()/foreach() help...
I have a small problem... I'm trying to select either the latest news (regular page) or all the news. heres my current script..
[code//Create bulletinsow function --bulletinshow("showallTRUE/FALSE");
function bulletinshow($all = false)
{
//create bulletins array
$bulletins = array
(
1 => bulletin("9.10.01", "-A", "For a couple months I have been promising a massive update to this site. However, after the events of September 11 (there's that date again...) I lost all inspiration to work on this site. I have now started a few personal projects that are demanding most of my time, and I'm not sure how long it will take me to get back to this site. I appreciate your visits, and I hope to return to your lives soon. Thanks!"),
2 => bulletin("19.9.02", "-Sky", "Well, well. I've modified the main page a little, i'm going to convert to a php include site layout. I might add a news ticker. I might even be able to get Sord to let me integrate into BattleSpot. as for rover sharing, i'm working on making a theme for my forums to apply to a MR category, where you'll be able to interact, etc, and able to upload/download rovers. Cheers!"),
3 => bulletin("24.12.02", "-Sky", "Bah. Haven't changed much lately, the site is now almost completely PHP. The SQL server was corrupted, so no more shouts for now. Or forums for that matter. I've been playing colobot for the last two days, and it's fun. <br /> On a side note, CMMM's Lutenint Whacker in Chaos Reigns is really cool. Battlespot has been fairly slow. That's it for now."),
4 => bulletin("13.1.03", "-Sky", "Finally got the database working again, but not for the forums. Were're changing to http://www.vxgames.net soon, so this will be http://www.mr.vxgames.net. Lata."),
);
while($current < $current3)
{
$current++;
if($all = true)
{
echo$bulletins[$current];
}
}
//shownew
if($all = false)
{
$current3 = count($bulletins);
echo $bulletins[$current3];
}
//showall
}
It doesn't cause any errors at least... not php ones, tho. I DO only want it to display the last $bulletins entry unless $all is true. Which it's not, It's just skipping along and putting all of them out there (according to zends debugger anyway)
Would someone suggest what I could do to prevent this?
[code//Create bulletinsow function --bulletinshow("showallTRUE/FALSE");
function bulletinshow($all = false)
{
//create bulletins array
$bulletins = array
(
1 => bulletin("9.10.01", "-A", "For a couple months I have been promising a massive update to this site. However, after the events of September 11 (there's that date again...) I lost all inspiration to work on this site. I have now started a few personal projects that are demanding most of my time, and I'm not sure how long it will take me to get back to this site. I appreciate your visits, and I hope to return to your lives soon. Thanks!"),
2 => bulletin("19.9.02", "-Sky", "Well, well. I've modified the main page a little, i'm going to convert to a php include site layout. I might add a news ticker. I might even be able to get Sord to let me integrate into BattleSpot. as for rover sharing, i'm working on making a theme for my forums to apply to a MR category, where you'll be able to interact, etc, and able to upload/download rovers. Cheers!"),
3 => bulletin("24.12.02", "-Sky", "Bah. Haven't changed much lately, the site is now almost completely PHP. The SQL server was corrupted, so no more shouts for now. Or forums for that matter. I've been playing colobot for the last two days, and it's fun. <br /> On a side note, CMMM's Lutenint Whacker in Chaos Reigns is really cool. Battlespot has been fairly slow. That's it for now."),
4 => bulletin("13.1.03", "-Sky", "Finally got the database working again, but not for the forums. Were're changing to http://www.vxgames.net soon, so this will be http://www.mr.vxgames.net. Lata."),
);
while($current < $current3)
{
$current++;
if($all = true)
{
echo$bulletins[$current];
}
}
//shownew
if($all = false)
{
$current3 = count($bulletins);
echo $bulletins[$current3];
}
//showall
}
It doesn't cause any errors at least... not php ones, tho. I DO only want it to display the last $bulletins entry unless $all is true. Which it's not, It's just skipping along and putting all of them out there (according to zends debugger anyway)
Would someone suggest what I could do to prevent this?
a single = assigns a value. You have to use == to compare two values.if($all = true)
if($all = true) first assigns TRUE to $all and then tests wether $all evaluates to TRUE (which is trivial for a boolean but there might have been if($all = 1) as well )
Same with the usual
Code: Select all
while ( $row = mysql_fetch_row($result) )since I have no function bulletin
I changed the script a bit
Code: Select all
<?php
function bulletinshow($all = FALSE)
{
//create bulletins array
$bulletins = array
(
array("9.10.01", "-A", "For a couple months I have been promising a massive update to this site. However, after the events of September 11 (there's that date again...) I lost all inspiration to work on this site. I have now started a few personal projects that are demanding most of my time, and I'm not sure how long it will take me to get back to this site. I appreciate your visits, and I hope to return to your lives soon. Thanks!"),
array("19.9.02", "-Sky", "Well, well. I've modified the main page a little, i'm going to convert to a php include site layout. I might add a news ticker. I might even be able to get Sord to let me integrate into BattleSpot. as for rover sharing, i'm working on making a theme for my forums to apply to a MR category, where you'll be able to interact, etc, and able to upload/download rovers. Cheers!"),
array("24.12.02", "-Sky", "Bah. Haven't changed much lately, the site is now almost completely PHP. The SQL server was corrupted, so no more shouts for now. Or forums for that matter. I've been playing colobot for the last two days, and it's fun. <br /> On a side note, CMMM's Lutenint Whacker in Chaos Reigns is really cool. Battlespot has been fairly slow. That's it for now."),
array("13.1.03", "-Sky", "Finally got the database working again, but not for the forums. Were're changing to http://www.vxgames.net soon, so this will be http://www.mr.vxgames.net. Lata."),
);
echo '<pre>';
if ($all === TRUE)
{
foreach($bulletins as $bulletin)
print_r($bulletin);
}
else
print_r($bulletins[count($bulletins)-1]);
echo '</pre>';
}
bulletinshow(FALSE);
echo '<hr/>';
bulletinshow(TRUE);
?>Well ok.. and here's my bulletin function...
I have it all configured so it displays readable html.
And Now that you've got a working version... I need help with.. (why did you use ===), foreach, and print_r.... the site is at http://www.vxgames.net/mr and the bulletins are displayed in the right section. Thanks volka
Code: Select all
function bulletin($date, $name, $content)
{
echo "\n\n <br /><br />";
echo "\n <font size='2' color='#33cc33'>";
echo "\n <b>Bulletin</b> </font><font size='1'>($date)<br />";
echo "\n $content";
echo "\n<i>-$name</i></font>";
}
?>And Now that you've got a working version... I need help with.. (why did you use ===), foreach, and print_r.... the site is at http://www.vxgames.net/mr and the bulletins are displayed in the right section. Thanks volka
your function echos the contents at the moment it is called.
Because of that reconsider
Because of that reconsider
$bulletins = array
(
1 => bulletin("9.10.01", "-A", "For a couple months I have been promising a massive update to this site. However, after the events of September 11 (there's that date again...) I lost all inspiration to work on this site. I have now started a few personal projects that are demanding most of my time, and I'm not sure how long it will take me to get back to this site. I appreciate your visits, and I hope to return to your lives soon. Thanks!"),
2 => bulletin("...
a debug session a day takes the trouble away 
really, it helps a lot (even more if you have an IDE that supports debugging) and is worth the time it takes to get used to.
http://www.php.net/manual/en/debugger.php
really, it helps a lot (even more if you have an IDE that supports debugging) and is worth the time it takes to get used to.
http://www.php.net/manual/en/debugger.php
- puckeye
- Forum Contributor
- Posts: 105
- Joined: Fri Dec 06, 2002 7:26 pm
- Location: Joliette, QC, CA
- Contact:
[quote="Sky"]Well ok.. and here's my bulletin function...
I think what Volka was trying to say is that you output the result instead of assigning it... Try the following:
Code: Select all
function bulletin($date, $name, $content)
{
echo "\n\n <br /><br />";
echo "\n <font size='2' color='#33cc33'>";
echo "\n <b>Bulletin</b> </font><font size='1'>($date)<br />";
echo "\n $content";
echo "\n<i>-$name</i></font>";
}
?>I think what Volka was trying to say is that you output the result instead of assigning it... Try the following:
Code: Select all
function bulletin($date, $name, $content)
{
$tmp = "\n\n <br /><br />";
$tmp .= "\n <font size='2' color='#33cc33'>";
$tmp .= "\n <b>Bulletin</b> </font><font size='1'>($date)<br />";
$tmp .= "\n $content";
$tmp .= "\n<i>-$name</i></font>";
return $tmp;
}
?>I use Zend's debugger... And it didn't tell me anything.
But your reply did, puckeye, thanks alot
I'm also having another probem.. it uses arrays and loops too >.<
But your reply did, puckeye, thanks alot
I'm also having another probem.. it uses arrays and loops too >.<
Code: Select all
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Student Self Test Template Maker</title>
</head><body>
<?
if(!$action)
{
$nextaction = 'buildarray';
}
elseif($action='buildarray')
{
if($HTTP_POST_VARS['questno']) {
$questnumber = $HTTP_POST_VARS['questno'];
}else{
$questnumber = $questno;
};
$selftestno = $HTTP_POST_VARS['questno'];
$current['q'] = 1;
$questno2 = $questnumber + 1;
echo($current['q']."<br>");
echo($questno2."<br>");
echo($questnumber."<br>");
while($current['q'] < $questno2)
{
$questionarray[$current['q']] = array($selftestno.'.'.'0'.$current['q']);
$current['q']++;
}
foreach($questionarray as $questions) {
echo($questions);
}
$nextaction='multichoice';
}
else {echo"";}?>
<form enctype='text/plain' action='<? echo ($_server["php_self"].$nextaction); ?>' method='post'>
<b>Name</b><input type='text' maxlength='20' name='studentname' value='<? echo($studentname); ?>'><br>
<b>Subject</b><input type='text' maxlength='10' name='subject' value='<? echo($subject); ?>'><br>
<b>Book#/Name</b><input type='text' maxlength='20' name='bookno' value='<? echo($bookno); ?>'><br>
<b>Questions #</b><input type='text' maxlength='5' name='questno' value='<? echo($questno); ?>'><br>
<b>self test #</b><input type='text' maxlength='5' name='selftestno' value='<? echo($selftestno); ?>'><br>
<input type='submit' value='next step'></form>
<?
/*
}
else{ echo "";}*/
/********************START DISCARDED <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>*************************/
//Time to build the array
/*if($action='buildarray')
{
$current['q'] = '1';
while(count($current['q']) <= ($questno+'1'))
{
$questionarray[$current['q']] = $selftestno.'.'.'0'.$current['q'];
$current['q']++;
}
//Loop to layout questions
if($current['q'] == $questions)
{
echo("<form enctype='text/plain' action='" . $_server['php_self'] . "' method='post'><input type='hidden' value='multichoice' name='action'><br><br>Please check all questions that are multiple Choice<br>");
$current['aq'] = '1';
$totalarrayquestions = count($questionarray);
while($current['aq'] <= $questionarray)
{
echo("<input type='checkbox' name='" . $questionarray[$current['aq']]['ismulti'] . "'><br>" . $selftestno . "." . "0" . $current['aq']);
}
}
}
*/
/*
elseif()
{
}
elseif()
{
}
elseif()
{
}
*/
?>
</body>
</html>Heres my new functions.
Code: Select all
//Create Bulletin function -- bulletin("date", "username", "content");
function bulletin($date, $name, $content)
{
if(!isset($bnum)) { $bnum='0'; }
$bnum++;
$bulletinindividual[$bnum] = "\n\n <br /><br />";
$bulletinindividual[$bnum] .= "\n <font size='2' color='#33cc33'>";
$bulletinindividual[$bnum] .= "\n <b>Bulletin</b> </font><font size='1'>($date)<br />";
$bulletinindividual[$bnum] .= "\n $content";
$bulletinindividual[$bnum] .= "\n<i>-$name</i></font>";
return $bulletinindividual;
}
//Create bulletinsow function --bulletinshow("showallTRUE/FALSE");
function bulletinshow($all = false)
{
//create bulletins array
/*
$bulletins = array
(
1 => */
bulletin("9.10.01", "-A", "For a couple months I have been promising a massive update to this site. However, after the events of September 11 (there's that date again...) I lost all inspiration to work on this site. I have now started a few personal projects that are demanding most of my time, and I'm not sure how long it will take me to get back to this site. I appreciate your visits, and I hope to return to your lives soon. Thanks!");
bulletin("19.9.02", "-Sky", "Well, well. I've modified the main page a little, i'm going to convert to a php include site layout. I might add a news ticker. I might even be able to get Sord to let me integrate into BattleSpot. as for rover sharing, i'm working on making a theme for my forums to apply to a MR category, where you'll be able to interact, etc, and able to upload/download rovers. Cheers!");
bulletin("24.12.02", "-Sky", "Bah. Haven't changed much lately, the site is now almost completely PHP. The SQL server was corrupted, so no more shouts for now. Or forums for that matter. I've been playing colobot for the last two days, and it's fun. <br /> On a side note, CMMM's Lutenint Whacker in Chaos Reigns is really cool. Battlespot has been fairly slow. That's it for now.");
bulletin("13.1.03", "-Sky", "Finally got the database working again, but not for the forums. Were're changing to http://www.vxgames.net soon, so this will be http://www.mr.vxgames.net. Lata.");
/* );
*/
//Showall
if($all == TRUE)
{
foreach($bulletinindividual as $bshow)
{
print_r($bshow);
}
}
//Shownew
if($all == FALSE)
{
print_r($bulletinindividual[$bnum]);
}
}heres one I revised based on you code volka. These STILL aren't putting ANYTHING out
Code: Select all
//Create Bulletin function -- bulletin("date", "username", "content");
function bulletin($date, $name, $content)
{
if(!isset($bnum)) { $bnum='0'; }
$bnum++;
$bulletinindividual[$bnum] = "\n\n <br /><br />";
$bulletinindividual[$bnum] .= "\n <font size='2' color='#33cc33'>";
$bulletinindividual[$bnum] .= "\n <b>Bulletin</b> </font><font size='1'>(".$date.")<br />";
$bulletinindividual[$bnum] .= "\n ".$content;
$bulletinindividual[$bnum] .= "\n<i>-".$name."</i></font>";
global $bulletinindividual;
global $bnum;
return $bnum;
return $bulletinindividual;
}
//Create bulletinsow function --bulletinshow("showallTRUE/FALSE");
function bulletinshow($all = FALSE)
{
//create bulletins array
/*
$bulletins = array
(
1 => */
bulletin("9.10.01", "-A", "For a couple months I have been promising a massive update to this site. However, after the events of September 11 (there's that date again...) I lost all inspiration to work on this site. I have now started a few personal projects that are demanding most of my time, and I'm not sure how long it will take me to get back to this site. I appreciate your visits, and I hope to return to your lives soon. Thanks!");
bulletin("19.9.02", "-Sky", "Well, well. I've modified the main page a little, i'm going to convert to a php include site layout. I might add a news ticker. I might even be able to get Sord to let me integrate into BattleSpot. as for rover sharing, i'm working on making a theme for my forums to apply to a MR category, where you'll be able to interact, etc, and able to upload/download rovers. Cheers!");
bulletin("24.12.02", "-Sky", "Bah. Haven't changed much lately, the site is now almost completely PHP. The SQL server was corrupted, so no more shouts for now. Or forums for that matter. I've been playing colobot for the last two days, and it's fun. <br /> On a side note, CMMM's Lutenint Whacker in Chaos Reigns is really cool. Battlespot has been fairly slow. That's it for now.");
bulletin("13.1.03", "-Sky", "Finally got the database working again, but not for the forums. Were're changing to http://www.vxgames.net soon, so this will be http://www.mr.vxgames.net. Lata.");
/* );
*/
//Showall
if($all === TRUE)
{
foreach($bulletinindividual as $bshow)
echo($bshow);
}
else
echo($bulletinindividual[count($bulletinindividual)-1]);
/*//Shownew
elseif($all == FALSE)
{
print_r($bulletinindividual[$bnum]);
}
else {echo "Bulletin Error";} */
}a good C-compiler would give you a 'statement unreachable'-warning for the second return. When the function returns it's done, no more statements of that function will be executed. Therefor you can only return one value (an array would be one value, too).global $bulletinindividual;
global $bnum;
return $bnum;
return $bulletinindividual;
global after you already used that variable ...hmmm...never tried that... but looks very strange.
Code: Select all
<?php
/** Create Bulletin function
@example bulletin("date", "username", "content");
*/
function bulletin($date, $name, $content)
{
$bulletinindividual = "\n\n <br /><br />";
$bulletinindividual .= "\n <font size='2' color='#33cc33'>";
$bulletinindividual .= "\n <b>Bulletin</b> </font><font size='1'>(".$date.")<br />";
$bulletinindividual .= "\n ".$content;
$bulletinindividual .= "\n<i>-".$name."</i></font>";
return $bulletinindividual;
}
/** Create bulletinsow function
@example bulletinshow(TRUE);
*/
function bulletinshow($all = FALSE)
{
//create bulletins array
$bulletins = array
(
bulletin("9.10.01", "-A", "For a couple months I have been promising a massive update to this site. However, after the events of September 11 (there's that date again...) I lost all inspiration to work on this site. I have now started a few personal projects that are demanding most of my time, and I'm not sure how long it will take me to get back to this site. I appreciate your visits, and I hope to return to your lives soon. Thanks!"),
bulletin("19.9.02", "-Sky", "Well, well. I've modified the main page a little, i'm going to convert to a php include site layout. I might add a news ticker. I might even be able to get Sord to let me integrate into BattleSpot. as for rover sharing, i'm working on making a theme for my forums to apply to a MR category, where you'll be able to interact, etc, and able to upload/download rovers. Cheers!"),
bulletin("24.12.02", "-Sky", "Bah. Haven't changed much lately, the site is now almost completely PHP. The SQL server was corrupted, so no more shouts for now. Or forums for that matter. I've been playing colobot for the last two days, and it's fun. <br /> On a side note, CMMM's Lutenint Whacker in Chaos Reigns is really cool. Battlespot has been fairly slow. That's it for now."),
bulletin("13.1.03", "-Sky", "Finally got the database working again, but not for the forums. Were're changing to http://www.vxgames.net soon, so this will be http://www.mr.vxgames.net. Lata.")
);
if($all === TRUE)
{ //Showall
foreach($bulletins as $bshow)
echo($bshow);
}
else
echo $bulletins[count($bulletins)-1];
}
bulletinshow(FALSE);
echo '<hr />';
bulletinshow(TRUE);
?>you're getting more and more understandable
+
And Zend is a php compiler... I think? (they helped make php)
And I went through a few versions of that last one... I tried global first, but I think it went into fits cause it wasn't set. And I didn't want to set it due to my bulletin function... But I think I finally got foreacH! So it goes for each one of $this put this 'facet' of $this out to $whatever, then do it again. Right?
And what does @ generate In comments
*Tries new script*
And Zend is a php compiler... I think? (they helped make php)
And I went through a few versions of that last one... I tried global first, but I think it went into fits cause it wasn't set. And I didn't want to set it due to my bulletin function... But I think I finally got foreacH! So it goes for each one of $this put this 'facet' of $this out to $whatever, then do it again. Right?
And what does @ generate In comments
*Tries new script*