Reputation: 575
I think this is probably really simple to do, but I must be overlooking something.
Basically, I have If/Else php statements, that check if a condition is true. Within each of these if statements, is a WHILE loop that echos all the appropriate rows from my database.
The while loops need to print some table cells, and fields from the row. At the moment, each WHILE loop has the code manually typed out, meaning if I want to change the layout or something else, I need to change it within each while loop.
What I want to do, is to define a variable, that I can echo, so that I only need to change the code in one place for it to affect all WHILE loops. However, the nature of the content within the WHILE loops mean that this wont work, and here is why:
//If the date is a monday
if($daycheck == "Monday") {
//Start a row and create cell with date in
echo "<tr><td height='100px' valign='top'>".$datedisplay."<br />";
//For each event found for this day
while($dayrow = mysql_fetch_assoc($dayresult)) {
//Show the event info
echo $dayrow['Event Time']."<br />".$dayrow['Event Name']."<br />";
}
//End the cell for the day
echo "</td>";
}
This is one IF statement, but imagine there are several more underneath. You can see in the middle is my WHILE loop, showing the fields from each row in the database.
The problem is, that if I stick:
$dayrow['Event Time']."<br />".$dayrow['Event Name']."<br />";
into a variable before the while loop, when I echo it out, it will be the same every time as the $dayrow parts don't get updated.
Is there any way that I can put the above into a variable so that it gets update each time the WHILE loop runs?
Perhaps if the $dayrow[''] parts were stored as text rather than variables somehow?
Thanks for any assistance you can offer.
Eds
Upvotes: 0
Views: 882
Reputation: 575
So here is what I have done using sprintf() that does what I need of it:
Set how I want it to display:
//Set the format to display the event details
$eventdisplay = "%s <br /> %s <br /><br />";
Set variables for time and name within each WHILE loop:
//Set the event time variable
$eventtime = $dayrow['Event Time'];
//Set the event name variable
$eventname = $dayrow['Event Name'];
Finally, echo it in the WHILE loop:
//Print event details into preset format
echo sprintf($eventdisplay, $eventtime, $eventname);
So this means I can set $eventdisplay anywhere in the document, and then the correct time and name get set within each loop. These are then formatted into the $eventdisplay variable using sprintf() and then echoed out,
Ultimate accomplishment:
I can change the layout of the event information in one place, and all loops will receive those changes.
Many thanks for everyone's help and suggestions
Eds
Upvotes: 0
Reputation: 2535
As DaveRandom suggested you can use sprintf()
or (more fancy and more possibilities) a template system using eval()
.
Upvotes: 1
Reputation: 2465
I would recommend for you to look for a good template system and use that instead of mixing your code and the visual representation.
e.g.: http://twig.sensiolabs.org/
Upvotes: 0
Reputation: 1488
what i understood about your question is simple. You do not want to update the variable in each iteration of loop but want to append the previous data , it's simple
$data="";
if($daycheck == "Monday") {
$data .= "<tr><td height='100px' valign='top'>".$datedisplay."<br />";
while($dayrow = mysql_fetch_assoc($dayresult)) {
$data .= $dayrow['Event Time']."<br />".$dayrow['Event Name']."<br />";
}
$data .= "</td>";
echo $data;
};
`
Upvotes: 0