In an effort to update the code on my website I am hitting walls, I am here to seek advice on different things. And I am not looking for elitist jerks to critisize my my lack of knowledge of php coding. For some its a steep learning curve and I am not over the hump yet.
The project that I have been trying to figure out the past couple of days is packing a number of variables into a line versus having serveral lines to do the same work.
If the variables are done indivually they work as they should. When bunched together they dont.. The empty variables echos come up blank on the page..
I don't follow external links while I'm at work, sorry. But if you can describe what you mean by bunched together?
I'd suggest that you use an associative array for this. But I'm not sure exactly how to build it since I'm not sure where you get these variables from in the first place. Can you post the code where you create the variables used in this line: <?php if (empty($processors) && empty($socket) && empty($ram) && empty($audio) && empty($video) && empty($expansion) && empty($partNumbers)) {?
did you try removing the parenthesis around the variables in the table layout?
Yes I have tried that and it does not work..
Quote:
But if you can describe what you mean by bunched together?
I am trying to simplify my code so that I can have less code do the same work. The section of code posted above is from a template file, and the variables are on other pages which link the .tpl file through an include. And the way it worked as originally coded was if a variable was found it would echo the results, and if no variable was found it would echo "Unknown".
The way the code was originally written the individual if/else code blocks interputed the variables and did show as intended.
I see so you do mean programatically bunched together and not visually.
An array is what you want for sure. I can biff one up out of what you have here, but it would be better to know where those variables are declared in the first place.
PHP Code:
<?php $aValues = array( 'supported processors' => &$processors, 'bus' => &$socket, 'chipset' => &$chipset, 'memory' => &$ram, 'video' => &$video, 'audio' => &$audio, 'expansion' => &$expansion, 'part number(s)' => &$partNumbers, ); // Referencing was used since I don't know if these exist, but now I don't need to bother with // an isset check. ?> <table class="tablestuff" width="75%" border="0" cellspacing="0" cellpadding=0 valign="top">
In your initial post, you cannot just check the if's like that without looping it or reassigning. That would mean you need to manually run it through an if/else or use a ternary for each variable. In an array, you could walk or map it. I personally just waited until it was time to output to determine.
And to further explain. To save on server space I created a template file to which I feed the variables into from .php files that link to the template though an include.
Not intending to "bump" the thread, just keeping the project in the same thread as I have questions.
I have another block of code in the .tpl file that works similar to the code above. In that if a variable exists something is echoed, if not something else is echoed...
The original code:
PHP Code:
<b>Found in the following systems:</b><br>
<b><font color="red">Compaq</b> - <i><?php if (isset($modelscompaq)) {
echo ($modelscompaq);}
else { echo "No information availible.";}?></i></font><br><font color="blue"><b>HP</b> - <i><?php if (isset($modelshp)) {
echo ($modelshp);}
else { echo "No information availible.";}?></i></font></p>font color="black"><b>HP</b> - <i><?php if (isset($modelsboth)) {
echo ($modelsboth);}
else { echo "No information availible.";}?></i></font>
The new code:
PHP Code:
<b>Found in the following systems:</b><br>
<?php
$bValues = array(
'Compaq' => &$modelscompaq,
'HP' => &$modelshp,
'Compaq/HP' => &$modelsboth,
);
// Referencing was used since I don't know if these exist, but now I don't need to bother with
// an isset check.
?>
<?php
The new code does work the same as the old code minus the color formatting which I would like to add into the new code (but not sure how, and have it comply with the xhtml standard which I am working towards). Also I want to have the new code only echo data that is availble or one line that states that no data is availible on the webpage, instead of three lines saying no data..
i.e. Found in the following systems:
[brand] - models
OR Found in the following systems:
No Data Availible
Yep you can do that quite easily. It requires a slightly different approach.
PHP Code:
<b>Found in the following systems:</b><br> <?php
$modelsboth = 'A model';
$bValues = array( 'Compaq' => &$modelscompaq, 'HP' => &$modelshp, 'Compaq/HP' => &$modelsboth, ); // Referencing was used since I don't know if these exist, but now I don't need to bother with // an isset check. ?> <?php
BTW, as for CSS you should look at replacing these markup tags with simple spans that have classes on them. <br /> can also be removed if you block it into a div which is a block type and will create a visually distinct linefeed. So that printf would look more like: <div><span class="strong">%s:</span>-<span class="emphasis">%s</span></div>. CSS can be applied to .strong and .emphasis to apply font weight and font style as well as any margin's required.
hmm that worked sort of.. Need to explain a bit better
Compaq (singular brand)
HP (singular brand)
Compaq/HP (post-merger Clusterbomb)
Back in 2002 (If I recall the year correctly) Compaq and HP merged. Prior to and after that time systems were sold under the Compaq and HP brands. The Compaq/HP "brand" came into play after the merger.
The way the code is written the output when no variables are availible: Found in the following systems: Compaq/HP: - A model
hmm that worked sort of.. Need to explain a bit better
Compaq (singular brand)
HP (singular brand)
Compaq/HP (post-merger Clusterbomb)
Back in 2002 (If I recall the year correctly) Compaq and HP merged. Prior to and after that time systems were sold under the Compaq and HP brands. The Compaq/HP "brand" came into play after the merger.
The way the code is written the output when no variables are availible: Found in the following systems: Compaq/HP: - A model
Sorry, that was for my testing. The first line of PHP code sets the value of $modelsboth = 'A value';. Simply remove that.
Thanks that worked nicely.. I tweaked the formatting just a hair. Still confused on the array_filter bit, why its needed. but not going to fret over it.
Array filter only returns the values that match the function provided. I wrote an anonymous function within it that checks for !empty, so it only returns values that are not empty. The $aFiltered will result in only what has a value set for the key, which may be anywhere from none of them to all of them.