I have a query and while loop that can be modified to display a variety of tables (e.g. Birds, Mammals, Flowers, etc.). However, each table requires special styling, so the page is getting bloated with code, like this…
switch($MyURL)
{
case 'State_Birds':
// INNER SWITCH(es)
switch($Symbol)
{
case 'bobwhite':
case 'turkey':
$Desig = '<span style="padding: 2px; background: #900; color: #fff;"><sup>G</sup></span>';
break;
case 'peregrine falcon':
$Desig = '<span style="padding: 2px; background: #f00; color: #fff;"><sup>R</sup></span>';
break;
case 'wood duck':
$Desig = '<span style="padding: 2px; background: #00f; color: #fff;"><sup>W</sup></span>';
break;
case 'Kirtland’s warbler':
$Desig = '<span style="padding: 2px; background: #f66;"><sup>B</sup></span>';
break;
case 'mourning dove':
$Desig = '<span style="padding: 2px; background: #0ff;"><sup>S</sup></span>';
break;
default:
$Desig = '';
break;
}
So I deleted all the code inside the while loop, pasted it in a separate file, then included it…
require($BaseINC."/2B/inc/D/Content/Child/DB/RefGS/PlanimalsCode.php");
But it doesn’t work - the code, that is. If I insert echo ‘Hello World’; on the included file, that works.
Can anyone tell me how to include a file inside a while loop?
Thanks.
You’re trying to define a function. So wrap it as such.
function sup($MyURL,$Symbol) {
//the code from above here
}
and then in the page when you need to call it…
sup($url,$symbol);
That said… if you’re doing a lot of this stuff, i’d suggest using CSS to define class types and use your switch to more intelligently assign things (or how about… class=‘$Symbol’ and dont use a switch at all?)
Thanks; those are good tips.
I guess I don’t understand your code after all. It’s supposed to go on the includED file, right? So I would link to the include as before…
require($BaseINC."/2B/inc/D/Content/Child/DB/RefGS/PlanimalsCode.php");
…then I would put your code in PlanimalsCode.php, where it would function in lieu of a PHP switch, right?
I get this error:
Cannot redeclare sup() (previously declared
But I don’t know how to implement your code correctly. This is obviously not right:
function sup($MyURL,$Symbol) {
switch($Symbol)
{
case 'bobwhite':
case 'turkey':
$Desig = '<span style="padding: 2px; background: #900; color: #fff;"><sup>G</sup></span>';
break;
case 'peregrine falcon':
$Desig = '<span style="padding: 2px; background: #f00; color: #fff;"><sup>R</sup></span>';
break;
case 'wood duck':
$Desig = '<span style="padding: 2px; background: #00f; color: #fff;"><sup>W</sup></span>';
break;
case 'Kirtland’s warbler':
$Desig = '<span style="padding: 2px; background: #f66;"><sup>B</sup></span>';
break;
case 'mourning dove':
$Desig = '<span style="padding: 2px; background: #0ff;"><sup>S</sup></span>';
break;
default:
$Desig = '';
break;
}
switch($IDArea)
{
case 'us-de':
case 'us-ri':
case 'us-sd':
$TRStyle = ' class="linkDark" style="background: #060; color: #fff;"';
break;
case 'us-wy':
$TDStyle = ' style="border-bottom: none;"';
break;
default:
$TRStyle = '';
break;
}
}
But if I replace $MyURL with the actual value, I get this…
function sup(State_Birds,$Symbol)
…which can’t work, either.
Thanks.
So how are you getting the values for $MyURL and $Symbol? Pulling them from a table, the URL string itself?
Yikes, I thought I replied, but I just checked and my response didn’t get published.
Anyway, the values are taken from a database, where values in a field named URL determine a pages URL ($MyURL). So if there’s a value “State_Birds,” a page will display at MySite/Topics/State_Birds.
So I could organize my code with a PHP switch based on URL’s…
switch($MyURL)
{
case 'State_Birds':
// CODE
break;
case 'State_Flowers':
// CODE
break;
default:
break;
}
…but that’s going to lead to code bloat. So I’d like to put the PHP switch in a separate file.
Sorry for the late responses; I don’t have an Internet connection at home, so I have to post from a library or WiFi cafe.
So… there’s two ways I could go with this;
#1: CSS.
One nice big CSS file with all the possible stylings.
.State_Birds_Warbler {
padding: 2px;
background-color: #900;
color: #fff;
}
and then something to the effect of;
echo “<span class='”.$MyURL.“_”.$Symbol.“'><sup>G</sup></span>”;
OR…
2: Database Driven
Add the following fields to your table:
background
color
symbol
select the extra fields when you select the data, and use that to construct your styling inline.