Page 1 of 1

Hi all, can you review this ?

Posted: Fri Aug 15, 2003 12:25 pm
by Draco_03
Hey, i just started at php yesterday, i m a designer with some sciences background, but i never did any programming befor (well i have good html knowledge)

so i started soimething to sort a list of clients, the code i did below works but i guess it's not very optimise. If you could help me optimise this code it would be nice :)

ps : what u see it's like the start, i ll slowly add stuff like i will change my array for a external texte file and i ll start adding button to add and modify the texte file...but i m starting slow :) )

thx
i'm happy cuz i did it with no help.. :D

code :

<?php
$zip = array( '330 renoir' => 'j6a 5p8',
'12 iutp' => 'h9a 3k3',
'154 trew' => 'i7k 5s6',
'12587 asss' => 'p8j 7r5',
'110 errie' => 'l9o 5d5'
);
$adress = array( '514 885-0965' => $zip,
'514 620-1609' => $zip,
'514 726-2978' => $zip,
'514 816-1609' => $zip,
'450 618-8788' => $zip
);
$names = array( 'a' => $adress,
'b' => $adress,
'c' => $adress,
'd' => $adress,
'e' => $adress
);
list ($rue, $codepostal) = each($zip);
list ($numeros) = each($adress);
list ($noms) = each($names);
ksort($names);
echo "<table width=\"350\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n<tr>\n\t<td height=\"20\"><strong>Noms</strong></td>\n<td height=\"20\"><strong>Num&eacute;ros</strong></td>\n<td height=\"20\"><strong>Rue</strong></td>\n<td height=\"20\"><strong>Code Postale</strong></td>\n</tr>\n";
reset($names);
reset($adress);
reset($zip);
while (list($noms) = each($names)) {
echo "<tr>\n\t<td>";
printf("%s", $noms);
echo "</td>\n<td>";
while (list($numeros) = each($adress)) {
printf("%s", $numeros);
echo "</td>\n<td>";
break;
}
while (list($rue, $codepostal) = each($zip)) {
printf("%s", $rue);
echo "</td>\n<td>";
printf("%s", $codepostal);
echo "</td>\n</tr>\n";
break;
}
}
echo "</table>";
?>

Posted: Fri Aug 15, 2003 2:58 pm
by McGruff
Well, I just learned something: I always thought that array key names in associative arrays had to follow the same naming rules as variables ( namely regex [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* - which you can test out with this handy tool http://www.weitz.de/regex-coach/#install
). Your array definitions did check out though, or at least the one I tested did.

However, I would recommend that tou DO follow the variable naming rules with associative arrays in case you ever want to declare the array values as vars.

Personally, I like to use foreach() rather than while(list/each) since it seems simpler and clearer, and may be slightly faster (nothing significant though). Foreach resets the internal array pointer so no need for reset() lines. Try rewriting the script with foreach - and lose all the list() and reset() lines.

It's better to keep html out of the script. One option is to include an html template where you echo out vars declared in the script, but see this thread viewtopic.php?t=10077 for a discussion of template engines.

I think I followed the same route to php as you. Although it's not too hard to learn to write php scripts, learning to write good scripts takes longer. In a sense anything that works is "good" but well-designed code saves you time in the long run since it's easier to maintain and change. We hope to have some new content on script design in the coming months.

Final tip: use the

Code: Select all

BB tags when you post code - makes it easier to read.

Posted: Fri Aug 15, 2003 3:51 pm
by Draco_03
hey thx a lot :)
i cheked the entry but i ll have to take some times to digest what was said :/ i still a real noob...

i ll give you the proof right now :)
i can make simploe foreach with no probs but when i want to indent foreach it s just don t give me the name of the variable..

i m working on the code right now i made the change that were obvious but i know i have a mistake...i ll find it eventually but i ll give you my code still :)

Code: Select all

<?php
$zip = array( '330 renoir' => 'j6a 5p8',
			  '12 iutp' => 'h9a 3k3',
			  '154 trew' => 'i7k 5s6',
			  '12587 asss' => 'p8j 7r5',
			  '110 errie' => 'l9o 5d5'
			);
$adress = array( '514 885-0965' => $zip,
				 '514 620-1609' => $zip,
				 '514 726-2978' => $zip,
				 '514 816-1609' => $zip,
				 '450 618-8788' => $zip
			   );
$names = array( 'a' => $adress, 
				'b' => $adress, 
				'c' => $adress, 
				'd' => $adress,
				'e' => $adress
			  );
reset($names);
reset($adress);
reset($zip);
ksort($names);
echo "<table width="350" border="0" cellpadding="0" cellspacing="0">\n<tr>\n\t<td height="20"><strong>Noms</strong></td>\n<td height="20"><strong>Num&eacute;ros</strong></td>\n<td height="20"><strong>Rue</strong></td>\n<td height="20"><strong>Code Postale</strong></td>\n</tr>\n";
foreach ($names as $noms) &#123;
	echo "<tr>\n\t<td>";
	echo "$noms";
	echo "</td>\n<td>";
		foreach ($adress as $numeros) &#123;
			echo "$numeros";
			echo "</td>\n<td>";
			break;
	&#125;
		foreach ($zip as $rue => $codepostal) &#123;
			echo "$rue";
			echo "</td>\n<td>";
			echo "$codepostal";
			echo "</td>\n</tr>\n";
			break;
	&#125;
&#125;
echo "</table>";
?>
oh and thx a lot again :)

Posted: Fri Aug 15, 2003 4:03 pm
by McGruff
You can loop through an array and get the values with:

Code: Select all

<?php
foreach($array as $value)
{
    // ..etc
}
?>
..or if you also want the key names:

Code: Select all

<?php
foreach($array as $key=>$value)
{
    // ..etc
}
?>

Posted: Fri Aug 15, 2003 4:14 pm
by m3rajk
first off there's a big defference between scripting and programming and markup languages. at their most basic use scripting is a fancy markup, and at their most complex scripting can be used in place of programming for a lot of things (unless we're talking about perl, which at the point other scripting languages fail it just hits lower edge of a slippery slope of increasing inefficiency where it's nothing short of ridiculuos to use due to the inefficiency)
php is one of the more powerful scripting languages that i have seen, but there's still a difference.

a good start would be to figure out what exactly you want to do then look for a book on it that uses php. it should be faster in teaching you than a site can be, and when the book fails to tell you of a php quirk, we're here for you

i think you should look into using a db. it looks like you're trying to use a txt file in place of one.

Posted: Sat Aug 16, 2003 1:05 am
by Draco_03
Thx a lot guys :)
yeah i m looking up in a book and yes i will be using a DB...but i want to get around with the code first that s why i m trying all of this :)

but i really appreciate your help :)

Posted: Sat Aug 16, 2003 12:05 pm
by derek
Yes, you can use foreach to print Array elements :

Code: Select all

<?
foreach($array as $temp1 => $temp2) 
&#123; 
   echo $temp1. " = ". $temp2."\n";

&#125; 
?>