php for loop trouble

Programming, for all ages and all languages.
Post Reply
cloudee1

php for loop trouble

Post by cloudee1 »

All right I know it has been a while since I have first undertaken the php plunge and I am still very much on the super low end of the learning curve ;D

I am trying to rewrite a website in php which formerly written in javascript accesses an array for it's information and then creates a webpage based off of the number of entries. Some day this information will probably be located in a database, that day is not today though. Here is an example of the javascript in action, and here is my newest not working php.

Code: Select all

<?php
$series_title='Template';
$publisher='Comic Gnome';
$description='The Comic Gnome welcomes you. A Resource of no Recourse';
$keywords='comic books, summaries, publisher';
$url_style='pics';
$url_code='html';
$image_url='pic';
$series_thoughts='My thoughts on this series as a whole have not entirely been formed yet, at least not so far as I would feel comfortable making them public.  But anyway, I highly doubt that you came here to read what it is that I may or may not have thought.';

$a[$i]='??'; 
$b[$i]='??'; 
$x[$i]='Writer';
$c[$i]='??'; 
$y[$i]='Penciller';
$d[$i]='??';
$z[$i]='Inks';
$e[$i]='??';
$f[$i]='A summary to comeA summary to comeA summary to ';
$g[$i]='A summary to comeA summary to comeA summary to ';

$a[$i]='??'; 
$b[$i]='??'; 
$x[$i]='Writer';
$c[$i]='??'; 
$y[$i]='Penciller';
$d[$i]='??';
$z[$i]='Inks';
$e[$i]='??';
$f[$i]='A summary to comeA summary to comeA summary to ';
$g[$i]='A summary to comeA summary to comeA summary to ';

$a[$i]='??'; 
$b[$i]='??'; 
$x[$i]='Writer';
$c[$i]='??'; 
$y[$i]='Penciller';
$d[$i]='??';
$z[$i]='Inks';
$e[$i]='??';
$f[$i]='A summary to comeA summary to comeA summary to ';
$g[$i]='A summary to comeA summary to comeA summary to ';

$v ="count($a)"; 

include('page_head.inc');  
?>

<body>
<a name=top></a>
<table id=main cellpadding=0 cellspacing=0 border=0>
<tr><td id="topleft"><?php echo("<img src=$url_style/spacer.gif width=174 height=101>"); ?></td>
      <td id="topright"><?php echo("<img src=$url_style/banner.gif width=271 height=100>"); ?></td></tr>
<tr><td id="marquee" colspan="2"><marquee scrollamount=4><script type="text/javascript">document.write(getmarquee());</script></marquee></td></tr> 
<tr><td id="navigation" rowspan="2">
       <table class="navigation" cellpadding=0 cellspacing=0>
   <?php  include("$url_style/page_navigation.inc");  ?>
   </table></td>
      <td id="content">
   <table class=news cellpadding=0 cellspacing=0>
   <tr class=info1>
         <td><?php echo("<img src=\"$url_style/info_1.gif\" height=30>"); ?></td>
         <td class=newstitle><?php echo("<span class=nobr>$series_title</span>"); ?></td>
         <td style="text-align:right;"><?php echo("<img src=\"$url_style\/info_3.gif\" height=30>"); ?></td></tr>
   <tr><td  class=info_content colspan=3><br><?php echo("$series_thoughts"); ?><br><br><hr></td></tr>
   <tr><td><span class=nobr>   Issues Owned: </span></td>
         <td align="center" width=100%><span class=nobr>
                  <a href=#??>??</a>, <a href=#??>??</a>
         </span></td>
         <td align="right"><span class=nobr><?php echo("           <i>of </i> $max_in_series  "); ?></span></td></tr> 
    </table>
<?php 
for ($i=0; $i<$v; $i++)      
{
echo("<a name=$a[$i]></a><br>");
    if( ($i==0) || ($i==2) || ($i==4) || ($i==6) || ($i==8) || ($i==10) || ($i==12) || ($i==14) || ($i==16) )
    {echo("
    <table class=left cellspacing=1 cellpadding=0>
    <tr class=dated><td class=left># $a[$i]</td>
    <td></td><td class=right>$b[$i]</td>
    <td rowspan=4><img alt=\"$series_title # $a[$i]\" src=pics/$image_url.$a[$i].jpg height=306 width=201></td></tr>
    ");} 
    else
    {echo("
    <table class=right cellspacing=1 cellpadding=0>
    <tr class=dated><td rowspan=4><img alt=\"$series_title # $a[$i]\" src=pics/$image_url.$a[$i].jpg height=306 width=201></td>
    <td>$b[$i]</td><td></td><td class=right># $a[$i]</td></tr>
     ");}
echo("<tr class=credittop><td class=credittop>$x[$i]</td><td class=credittop>$y[$i]</td><td class=credittop>$z[$i]</td></tr>");
echo("<tr class=creditbottom><td class=creditbottom>$c[$i]</td><td class=creditbottom>$d[$i]</td><td class=creditbottom>$e[$i]</td></tr>");
echo("<tr><td colspan=3 align=center><b>$f[$i] $g[$i]</b></td></tr>");
echo('</table>');
} 
?>
<hr>
<?php  include('page_footer.inc');  ?>
</BODY>
</HTML> 
Everything seems to be working, except for that "for if else" statement. I am not sure what it is that is amiss, if anyone has any suggestions.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:php for loop trouble

Post by distantvoices »

you're adding elements to the arrays, aren't you?

Code: Select all

$a[$i]='??'; 
$b[$i]='??'; 
$x[$i]='Writer';
$c[$i]='??'; 
$y[$i]='Penciller';
$d[$i]='??';
$z[$i]='Inks';
$e[$i]='??';
$f[$i]='A summary to comeA summary to comeA summary to ';
$g[$i]='A summary to comeA summary to comeA summary to ';
I reckon doing so should be done this way: $a[0]="blah"; for the first element.
Well, but then, I've never attempted at doing such a thing in this way. I'd use a class/struct for the info you want to store and display.

You better tell us what this intends to do for I miss the initialization of $i to a given value. Comments might be nice, hm?

IN your iteration stuff, I see loads of if & ||, which test for even numbers, is this right?

Why not say

Code: Select all

if(!(x%2)){bleh...;}
STay safe. I'll keep checking the code, that's it for now. :-)

[edit:]
howabout initializing and incrementing $i?

Code: Select all

$i=0;
$a[$i]='??'; 
$b[$i]='??'; 
$x[$i]='Writer';
$c[$i]='??'; 
$y[$i]='Penciller';
$d[$i]='??';
$z[$i]='Inks';
$e[$i]='??';
$f[$i]='A summary to comeA summary to comeA summary to ';
$g[$i]='A summary to comeA summary to comeA summary to ';

$i++;
$a[$i]='??'; 
$b[$i]='??'; 
$x[$i]='Writer';
$c[$i]='??'; 
$y[$i]='Penciller';
$d[$i]='??';
$z[$i]='Inks';
$e[$i]='??';
$f[$i]='A summary to comeA summary to comeA summary to ';
$g[$i]='A summary to comeA summary to comeA summary to ';

$i++;
$a[$i]='??'; 
$b[$i]='??'; 
$x[$i]='Writer';
$c[$i]='??'; 
$y[$i]='Penciller';
$d[$i]='??';
$z[$i]='Inks';
$e[$i]='??';
$f[$i]='A summary to comeA summary to comeA summary to ';
$g[$i]='A summary to comeA summary to comeA summary to ';
[/edit]

Helps?
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
cloudee1

Re:php for loop trouble

Post by cloudee1 »

you're right comments would be nice :P sorry bout that.

I have added the line ... $i='0'; to my copy, right at the top of the first array, I swear it used to be there, I don't think it needs the $i++ because I think php is supposedly smart enough to understand the increase, but I still tried adding them. Unfortunately to no avail. But regarding changing the arrays to not include the $i, don't I need to name the array value so that I can check against it with the ... for ($i=0; $i<$v; $i++) statement, but then again that is why I'm looking for help, cause I don't really know ;).

About the or statements, there shouldn't be more than 15 seperate array entries, so at the time that seemed the fastest way to determine between an odd or even [$i], depending on which it is, one of two types of tables are supposed to get generated. - so does "if(!(x%2))" do essentially the same thing, if theres a remainder it returns true, no remainder return false?

Thanks for helping, getting this to work right would be great.
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:php for loop trouble

Post by distantvoices »

Code: Select all

if(!(x%2)){...}
This one returns true if no remainder and false, if remainder. It's just the inverse of if(x%2). YOu can use either of them, be careful where you place the statements you want the computer to execute in each respective case.

BTW: it's easier to read than all those or's which basically do the same.

regarding the for loop: it should work. I Reckon you simplify the thing and put in some echo $i and so forth at crucial places - so you 'll get it sorted out easily, eh?

I'll continue looking, maybe there's something hidden in the dark.

Ah: could you tell what the program does successfully and what it fails to accomplish? This might be of help.

*chris could tell better what's wrong, my php is a wee bit rusty - being osdeveloper doesn't help really *rofl**

ps:

Code: Select all

$v ="count($a)"; 
in all earnest I long to grab throu the ethernet fiber at your earlob and twist a bit. *rofl* this one should be:

Code: Select all

$v=count($a);
or you save $i in $v after assigning all the array members. :-)

I don't want to know what php does in case one dares to compare string(apples) with integer(pears). *rofl*
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:php for loop trouble

Post by Solar »

cloudee1 wrote: I don't think it needs the $i++ because I think php is supposedly smart enough to understand the increase...
No it isn't. Computer languages very rarely are any good at reading programmer's minds like that... you have to tell them every minute step. Unless you're coding LISP, of course. 8)


Always assume the computer is a dumb block of metal and plastic. Because that's what it is. ;)
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:php for loop trouble

Post by Candy »

Solar wrote:
cloudee1 wrote: I don't think it needs the $i++ because I think php is supposedly smart enough to understand the increase...
No it isn't. Computer languages very rarely are any good at reading programmer's minds like that... you have to tell them every minute step. Unless you're coding LISP, of course. 8)
Some languages try to help..

Prolog automatically tries all possible solutions for you if you want more than one, and only gives those you don't filter out... IE, don't specify loop stuff, just tell it what you want

C# has a nice loop variant, foreach. Similar to other languages, you just do a

Code: Select all

foreach(int x in array) { ... something with x  ... }
and it loops for each item in the array. Downside, it's a Microsoft-invention (not portable) and it's around 1/5th of the speed of a plain incremental loop. Still, it looks nice...
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:php for loop trouble

Post by distantvoices »

@solar: iirc, php does implicit index increment in case one omits the index inside the brackets. eg:

Code: Select all

  $a[]="eins";
  $a[]="zwei";
  $a[]="drei";
should produce an array with three members.

But I daresay never trust anything attempting to be clever without you being able to see where it has its brain.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:php for loop trouble

Post by Solar »

Candy wrote:
C# has a nice loop variant, foreach. [...] Downside, it's a Microsoft-invention (not portable) and it's around 1/5th of the speed of a plain incremental loop.
Again Microsoft managed to sell something as "invention" just because other people were not as vocal about the same feature earlier. [tt]std::for_each( container.begin(), container.end(), doSomething )[/tt] is standard C++.

But if you explicitly give $i, you should explicitly increment $i. No, you should always explicitly increment $i, I agree with BI in this regard. ;)
Every good solution is obvious once you've found it.
User avatar
Candy
Member
Member
Posts: 3882
Joined: Tue Oct 17, 2006 11:33 pm
Location: Eindhoven

Re:php for loop trouble

Post by Candy »

Solar wrote:
Candy wrote:
C# has a nice loop variant, foreach. [...] Downside, it's a Microsoft-invention (not portable) and it's around 1/5th of the speed of a plain incremental loop.
Again Microsoft managed to sell something as "invention" just because other people were not as vocal about the same feature earlier. [tt]std::for_each( container.begin(), container.end(), doSomething )[/tt] is standard C++.

But if you explicitly give $i, you should explicitly increment $i. No, you should always explicitly increment $i, I agree with BI in this regard. ;)
IIRC you can't just include a bunch of code in the C++ foreach, it'd have to be a function call or something like that. But of course, Perl and other such languages (PHP I think as well) have done it first, as do some experimental academic languages that are up to 30 years old.

Same with all the things Microsoft "invents".
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:php for loop trouble

Post by Colonel Kernel »

Candy wrote:Downside, it's a Microsoft-invention (not portable)
Really? Is it not in the C# ECMA standard? I'd be really surprised if I couldn't use foreach on Mono....
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:php for loop trouble

Post by Solar »

Candy wrote: IIRC you can't just include a bunch of code in the C++ foreach, it'd have to be a function call or something like that.
A functor, i.e. an object for which operator() is overloaded so that, for a functor foo, you can call foo() to do something.

Which has two advantages - the functor can conveniently retain private state you don't have to bother about, and the compiler can inline the code quite efficiently. Which is why a C++ sort() regularily embarrases C's qsort() by no small margin. ;)
Every good solution is obvious once you've found it.
User avatar
Colonel Kernel
Member
Member
Posts: 1437
Joined: Tue Oct 17, 2006 6:06 pm
Location: Vancouver, BC, Canada
Contact:

Re:php for loop trouble

Post by Colonel Kernel »

Solar wrote: r, i.e. an object for which operator() is overloaded so that, for a functor foo, you can call foo() to do something.

Which has two advantages - the functor can conveniently retain private state you don't have to bother about, and the compiler can inline the code quite efficiently. Which is why a C++ sort() regularily embarrases C's qsort() by no small margin. ;)
It's also why C++ desperately needs proper lambda expressions IMO (I've tried boost lambda and it just about made my head explode). :P
Top three reasons why my OS project died:
  1. Too much overtime at work
  2. Got married
  3. My brain got stuck in an infinite loop while trying to design the memory manager
Don't let this happen to you!
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:php for loop trouble

Post by Solar »

Colonel Kernel wrote: It's also why C++ desperately needs proper lambda expressions IMO (I've tried boost lambda and it just about made my head explode). :P
That happens when you try to squeeze too many paradigms into one language. C++ is procedural, object oriented, and supports generic programming. It's also upwards compatible to the most common programming language of its time (C).

It's a small marvel you can add lambda functions (which stem from functional programming) to C++ without requiring additional keywords... but such marvels come at a cost.

8)
Every good solution is obvious once you've found it.
cloudee1

Re:php for loop trouble

Post by cloudee1 »

$v ="count($a)";

in all earnest I long to grab throu the ethernet fiber at your earlob and twist a bit. *rofl* this one should be:

$v=count($a);
So the ear tweaking was apparantly exactly what was needed, after removing the quotes, it appears to be working perfectly. ;)

Also I have tried it with and without and you do in fact need to increment $i++

thank you.
Post Reply