Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-19-2012, 06:58 AM   PM User | #1
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
Advice on php code improvements

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..

Code:
<table valign="top" style="border: 0px; background color: transparent; float: right; width: 58%;">
<tr><td class=submenuheading><p align="left">
	<table class="tablestuff" width="100%" border="0" cellspacing="0" cellpadding="0" align="left" valign="top">
<tr><td class="submenuheading"><p align="left">
<?php if (empty($processors) && empty($socket) && empty($ram) && empty($audio) && empty($video) && empty($expansion) && empty($partNumbers)) {
echo "Data Not Availible"; }
else {
?>
   <table class="tablestuff" width="75%" border="0" cellspacing="0" cellpadding=0 valign="top">
	<tr>
	<td colspan="2"><b>Supported Processors: </b>
	</td>
	<td>
	</td>
	</tr>
	<td>
	</td>
	<td colspan="2">
	<p><?php echo ($processors);?></p>	
	</td>
	</tr>
 
	<tr>
	<td colspan="2"><b>Bus: </b>
	</td>
	<td>
	</td>
	</tr>
	<td>
	</td>
	<td colspan="2">
	<p><?php echo ($socket);?></p></td>
	</tr>
 
	<tr>
	<td colspan="2"><b>Chipset: </b>
	</td>
	<td>
	</td>
	</tr>
	<td>
	</td>
	<td colspan="2">
	<p><?php echo ($chipset);?></p></td>
	</tr>
 
	<tr>
	<td colspan="2"><b>Memory: </b>
	</td>
	<td>
	</td>
	</tr>
	<td>
	</td>
	<td colspan="2">
	<p><?php echo ($ram);?></p></td>
	</tr>
 
	<tr>
	<td colspan="2"><b>Video: </b>
	</td>
	<td>
	</td>
	</tr>
	<td>
	</td>
	<td colspan="2">
	<p><?php echo ($video);?></p></td>
	</tr>
	<tr>
	<td colspan="2"><b>Audio: </b>
	</td>
	<td>
	</td>
	</tr>
	<td>
	</td>
	<td colspan="2">
	<p><?php echo ($audio);?></p></font>
</td>
	</tr>
	<tr>
	<td colspan="2"><b>Expansion: </b>
	</td>
	<td>
	</td>
	</tr>
	<td>
	</td>
	<td colspan="2">
	<p><?php echo ($expansion);?></p></td>
	</tr>
 
	<tr>
	<td colspan="2"><b>Part Number(s): </b>
	</td>
	<td>
	</td>
	</tr>
	<td>
	</td>
	<td colspan="2">
	<p><?php echo ($partNumbers);}?></p></td>
	</tr>
 
	<tr>	
	<td width="10%">	
	</td>
	<td>
	</td>
	<td width="10%">	
	</td></tr></table>
http://www.ctechinfo.net/boards/asus/berkeley.php

Thanks in advance..
Ctechinfo is offline   Reply With Quote
Old 09-19-2012, 07:04 AM   PM User | #2
c1lonewolf
Regular Coder

 
Join Date: Sep 2002
Posts: 216
Thanks: 0
Thanked 11 Times in 11 Posts
c1lonewolf is an unknown quantity at this point
did you try removing the parenthesis around the variables in the table layout?
__________________
NO Limits!!
c1lonewolf is offline   Reply With Quote
Old 09-19-2012, 03:43 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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)) {?
Fou-Lu is offline   Reply With Quote
Old 09-19-2012, 08:12 PM   PM User | #4
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
Quote:
Originally Posted by c1lonewolf View Post
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.

Bunched together
PHP Code:
<?php if (empty($processors) && empty($socket) && empty($ram) && empty($audio) && empty($video) && empty($expansion) && empty($partNumbers)) {?
Seperated:
PHP Code:
<?php
if (isset($processors)) {
echo (
$processors); }
else { echo 
"No Data Availible";}?>
Ctechinfo is offline   Reply With Quote
Old 09-19-2012, 08:30 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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">

<?php

foreach ($aValues AS $name => $value)
{
?>
    <tr>
    <td colspan="2"><b><?php echo ucwords($name);?>: </b>
    </td>
    <td>
    </td>
    </tr>
    <td>
    </td>
    <td colspan="2">
    <p><?php echo !empty($value) ? $value 'Unknown';?></p>    
    </td>
    </tr>
<?php
}
?>
    <tr>    
    <td width="10%">    
    </td>
    <td>
    </td>
    <td width="10%">    
    </td></tr></table>
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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
Ctechinfo (09-20-2012)
Old 09-20-2012, 06:36 AM   PM User | #6
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
That worked nicely.. As I said in my initial post I'm still on the uphill side of the learning curve..
Ctechinfo is offline   Reply With Quote
Old 09-20-2012, 06:56 AM   PM User | #7
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
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.

The code for the example page listed above:
PHP Code:
<?php  
$page_title 
"Berkeley - Compaq/HP OEM Asus IPIBL-LA Motherboard"
$page_description "Spec sheet and downloads for the Compaq/HP Berkeley motherboard"
$keywords "Compaq, HP, Asus, Pegatron, IPIBL-LA, Berkeley, motherboard, spec sheet, downloads, information"
$page_name "Berkeley";
$image '"/images/ipibl-la.jpg"';
$processors "Intel Core 2 Quad Q6xx0 w/ Quad Core technology upto Q6600 (Kentsfield)<br>Core 2 Duo E6x00 upto E6850 or E6700 (Conroe)<br>Core 2 Duo E4x00 upto E4400 (Conroe)";
$socket "Socket 775 - 800/1066/1333MHz FSB";
$chipset "Intel G33 Chipset";
$ram "Four DDR2 DIMM Slots<br>DDR2-667/DDR2-800<br>8GB (64-bit)<br>4GB (32-bit)";
$video "Integrated<br>PCI Express x16 Graphics Cards";
$audio "Integrated High Definition Audio<br>Audio CODEC: Realtek ALC888S";
$expansion "One PCI Express x16 Slot<br>Two PCI Express x1 Slots<br>One PCI Slot";
$bdocs '"/manuals/berkely_manual.pdf"';
$bdocs_descript "Motherboard Manual";
$retail "Asus IPIBL-LA";
$biosver "a Compaq or an HP";
$f_link '" /forum/viewtopic.php?t=391"'
include (
$_SERVER['DOCUMENT_ROOT'].'/templates/board.tpl'); 
?>
Ctechinfo is offline   Reply With Quote
Old 09-20-2012, 04:44 PM   PM User | #8
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
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

foreach ($bValues AS $name => $value)
{
?>
<b><?php echo ucwords($name);?>: </b> - <i><?php echo !empty($value) ? $value 'No Data Availible';?></i><br />
<?php
}
?>
</p>
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
Ctechinfo is offline   Reply With Quote
Old 09-20-2012, 06:35 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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

$aFiltered 
array_filter($bValuescreate_function ('$a''return !empty($a);'));
if (empty(
$aFiltered))
{
    print 
'<i>No Data Available</i>';
}
else
{
    foreach (
$aFiltered AS $name => $value)
    {
        
printf('<b>%s: </b> - <i>%s</i><br />' PHP_EOLucwords($name), $value); 
    }
}
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.

Last edited by Fou-Lu; 09-20-2012 at 06:38 PM..
Fou-Lu is offline   Reply With Quote
Old 09-20-2012, 08:23 PM   PM User | #10
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
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
Ctechinfo is offline   Reply With Quote
Old 09-20-2012, 08:25 PM   PM User | #11
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Quote:
Originally Posted by Ctechinfo View Post
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.
Fou-Lu is offline   Reply With Quote
Old 09-20-2012, 08:59 PM   PM User | #12
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
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.


will be back with more questions I'm sure..
Ctechinfo is offline   Reply With Quote
Old 09-20-2012, 09:23 PM   PM User | #13
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,662
Thanks: 4
Thanked 2,452 Times in 2,421 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
Fou-Lu is offline   Reply With Quote
Old 09-20-2012, 09:52 PM   PM User | #14
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
Thanks for the info..
Ctechinfo is offline   Reply With Quote
Old 09-29-2012, 06:54 PM   PM User | #15
Ctechinfo
New Coder

 
Join Date: Sep 2012
Posts: 88
Thanks: 3
Thanked 3 Times in 3 Posts
Ctechinfo is an unknown quantity at this point
Apparently I encoutered a stimbling block...

With the last array "project"

If no data is availible at all. The code works as desired outputting

Found in the following sytems:
No Data Availible

With any data availible, the code outputs

Found in the following systems:
Compaq - Presario xxxxx
HP -
Compaq/HP -

If there is data availble I want only what those areas to get outputted, for instance..

Board X
Found in the following systems:
Compaq - Presario xxxx
HP - Pavilion xxxx
Compaq/HP - HP Compaq xxxx

Board Y
Found in the following systems:
Compaq - Presario xxxx
HP - Pavilion xxxx

Board Z
Found in the following systems:
Compaq - Presario xxxxx

Thanks
Ctechinfo is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:09 AM.


Advertisement
Log in to turn off these ads.