ALL :: ZONES :: Loop and print arrays |
|||
| By: PHP newbee |
Date: 30/05/2003 00:00:00 |
Points: 300 | Status: Answered Quality : Excellent |
|
I have arrays I have and I set them from start then I have a loop that match up arrays then print them out . all the 1 arrays them all the 2 arrays and so on. only it is not working,help me plz. <HTML> <HEAD> <TITLE> MYC </TITLE> </HEAD> <BODY> <? $Text[1] ='Lab one'; $Link[1] ='labalone.php'; $Desc[1] = 'e reports; $Text[2] = progress $Link[2] ='progressreport.php'; $Desc[2] = Progress report for Nuc.'; //*for loop*// for ($x = 0; $x <= count($Text); $x++); $Var=link[$x] = $curLink=$link[$x]; $curDesc=$desc[$x]; print "<a name=$curlink></a>\n". "<b>$curDesc</b>\n\n". "<blockquote>\n"; } ?> </BODY> </HTML> |
|||
| By: ThG | Date: 30/05/2003 02:59:00 | Type : Answer |
|
There are some serious syntax errors in your code. 1) make sure strings are always enclosed in ' ', for example $Desc[1] and $Desc[2] are not correctly declared, and the result is something you don't want surely. 2) PHP is case sensitive, $Desc is not the same as $desc 3) Usually (but this is NOT mandatory) arrays start from index 0 and goes to count($array) - 1, so you can use the condition $x < count($arr) in the for loops.. 4) for() body must be enclosed in { } I suggest you to read <A HREF="http://fr2.php.net/manual/en/language.basic-syntax.php">http://fr2.php.net/manual/en/language.basic-syntax.php</a> Here there is your code fixed code: <? $Text[0] ='Lab one'; $Link[0] ='labalone.php'; $Desc[0] = 'e reports'; $Text[1] = 'progress'; $Link[1] ='progressreport.php'; $Desc[1] = 'Progress report for Nuc.'; //*for loop*// for ($x = 0; $x < count($Text); $x++) { $curLink = $Link[$x]; $curDesc = $Desc[$x]; print "<a name=$curLink></a>\n". "<b>$curDesc</b>\n\n". "<blockquote>\n"; } ?> |
|||
| By: datibbaW | Date: 30/05/2003 03:02:00 | Type : Assist |
|
| PHPnewbee: Are you sure that code won't give you parse errors? Maybe you can put these three array into one, and get a code like this: <? $a=Array( Array( 'text'=>'lab one', 'link'=>'labalone.php', 'desc'=>'e reports' ), Array( 'text'=>'progress report', 'link'=>'progressreport.php', 'desc'=>'Progress report for Nuc.' ) ); foreach ($a as $item) { // $item consists of three elements: $item['text'], $item['link'] and $item['desc'] echo '<a name="',htmlspecialchars($item['link']),'"></a><b>',htmlspecialchars($item['descr']),'</b><br />'; } ?> |
|||
| By: VGR | Date: 30/05/2003 03:03:00 | Type : Assist |
|
| moreover, $Var=link[$x] = is invalid for two to four reasons : terminator, $ missing and right value missing, plus the a=b=c which is not what you wanted, for sure. as for the arrays beginning at 1, I encourage you to do so, it's sooooo more logical 8-) typing for ($x=1;$x<=count($thearray);$x++) {} is no longer and much more practical : you'll NEVER wonder "does it begin at zero or at one?". People in the real worls NATURALLY count from 1 to N, not from 0 to N-1 it's one of the stupidities introduced by C with its strings (or more precisely, zero-based arrays of char) |
|||
| By: ThG | Date: 30/05/2003 03:16:00 | Type : Comment |
|
VGR: Unfortunately those "stupids" (as you call them) PHP coder (PHP is written in C, did you know?) made array_* functions optimized to work with arrays starting from index 0. Of course you can make it work with broken arrays, but one day you will waste your time figuring out why some big script doesn't work :-) |
|||
| By: VGR | Date: 30/05/2003 04:17:00 | Type : Comment |
|
| don't make me say what I haven't said :D "stupidities introduced by C" and "PHP coders" have nothing to do in common. I sometimes use array[0..N] when I have to deal with ZinDoze ugly structures, but the most natural way of programming is the way people understand the most naturally :D you've N elements ? Good. Use $thearray[1..N] exactly as rastaX did before you (or an other smart C coder :D ) begun bugging and losing him ;-) |
|||
| By: datibbaW | Date: 30/05/2003 06:29:00 | Type : Comment |
|
| Surely Turbo Pascal probably introduced a more intuitive way to use arrays. However, the implementation of C is more efficient in clock cycles; and hey, do programmers really live in the real world? ;) In my opinion the use of foreach (php >= 4) is the most intuitive to use since you don't have to bother about the type of keys used (strings or numbers) as well as the base of numbering. |
|||
| By: VGR | Date: 30/05/2003 06:40:00 | Type : Comment |
|
| wrong. The faster compiler is Turbo-Pascal. The better optimizer too :D C code is not optimized "per se", it's badly inherently bug-prone, that's a difference. And if it comes to low-level assembler coding, Turbo-Pascal also permits it and in fact uses a lot of it in its implementation 8-) the only difference is that it's fast, efficient, but ***also*** robust and readable :D and turbo-Pascal introduced nothing. An array is whatever-based. An array of chars too. String are (were) short ones so counting from 1 to N with element [0] containing the length was (and is still) also the most efficient way :D It's the fastest, too 8-)) C coders have to count incrementally until they encounter #0 :D :D arf arf arf what a waste of time. |
|||
| By: harwantgrewal | Date: 31/05/2003 05:07:00 | Type : Assist |
|
| uffffff.. to much little silly errors in his code.. I am thinking how he able to make things working :) here is change code of mine. <? $Text[0] ='Lab one'; $Link[0] ='labalone.php'; $Desc[0] = 'e reports'; $Text[1] = 'progress'; $Link[1] ='progressreport.php'; $Desc[1] = 'Progress report for Nuc.'; //*for loop*// for ($x = 0; $x <= count($Text); $x++){ print "<a name=$Link[$x]><b>$Desc[$x]</b></a> \n"; } ?> Cheers Harry |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








