Unstoppable Domains

PHP Repeat Code Question

Spaceship Spaceship
Watch

NetworkTown.Net

Account Closed
Impact
2
Hi

I have a quick question, i have the following:

PHP:
<?php

$num_displayed = '3';

?>

Then i have a code like this:

PHP:
 <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="greyBox" width=50%> 
<tr> 
  <td> <table border="0" cellpadding="0" cellspacing="0"> 
      <tr> 
        <td width=100%> <table border="0" cellpadding="0" cellspacing="0" width=100%> 
            <tr> 
              <td align=left class="thead" colspan="5" height=20> <strong><?php    echo  '<a href="view.php?id='.$id.'">'; echo"$title"; echo '</a>'; ?></strong></td> 
               </tr> 
          </table> 
          <p class="normalText" valign=top><?php echo"$description"; ?></p></td> 
      </tr> 

    </table></td>

I want this above code to be repeated to the number in $num_displayed so say that in $num_displayed it $num_displayed = '3'; this above code would be repeated 3 times, dose any one know if php can do this and were i can find the code.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
A couple of things:

Firstly, when using numbers as numbers (as opposed to as strings), don't use quotes. It will generally work with the quotes still, but you're using bad coding practices and you'll shoot yourself in the foot eventually.

Secondly, you want a for loop.

Something like:
PHP:
$num_displayed = 3; 

for ($i = 0; ($i < $num_displayed); $i++) {
?>
    <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="greyBox" width=50%> 
    <tr> 
    <td> <table border="0" cellpadding="0" cellspacing="0"> 
    <tr> 
    <td width=100%> <table border="0" cellpadding="0" cellspacing="0" width=100%> 
    <tr> 
    <td align=left class="thead" colspan="5" height=20> <strong><?php    echo  '<a href="view.php?id='.$id.'">'; echo"$title"; echo '</a>'; ?></strong></td> 
    </tr> 
    </table> 
    <p class="normalText" valign=top><?php echo"$description"; ?></p></td> 
    </tr> 

    </table></td>  
<?php
}
 
0
•••
I would probably use a while loop actually here:

PHP:
$num_displayed = 3;
$i = 0;
while($i<$num_displayed){
?>
    <table width="100%"  border="0" cellpadding="0" cellspacing="0" class="greyBox" width=50%> 
    <tr> 
    <td> <table border="0" cellpadding="0" cellspacing="0"> 
    <tr> 
    <td width=100%> <table border="0" cellpadding="0" cellspacing="0" width=100%> 
    <tr> 
    <td align=left class="thead" colspan="5" height=20> <strong><?php    echo  '<a href="view.php?id='.$id.'">'; echo"$title"; echo '</a>'; ?></strong></td> 
    </tr> 
    </table> 
    <p class="normalText" valign=top><?php echo"$description"; ?></p></td> 
    </tr> 

    </table></td>  
<?php 
$i++;
}
 
0
•••
What dose the
PHP:
$i = 0;
do?
 
0
•••
NetworkTown.Net said:
What dose the
PHP:
$i = 0;
do?

Its used for counting the iterations of the loop
 
0
•••
NetworkTown.Net said:
What dose the
PHP:
$i = 0;
do?
it basically sets the value of $i.

You need it to, as mikor said, count how many times the while loop has gone through.
 
0
•••
TwistMyArm said:
you want a for loop.
PoorDoggie said:
I would probably use a while loop actually here:
Just pointing out to NetworkTown.Net,

Either of those would be fine, I probably would have stuck with the FOR loop as it's just that tiny bit quicker to write ;).

But both will do exactly what you want, neither have any noticeable up or downside in this case.

All the best,
Rhett.
 
0
•••
ye, sorry - lol - I would personally use a while loop - not because it is better, but because it is just my preference. It is more "logical" imo. I have problems with the logical side of php, hehe - its easier if I can go through and read it out, like "while *something* do this, then increase $i then go back to the top and do *something* again..." etc! :D hehe
 
0
•••
PoorDoggie said:
ye, sorry - lol - I would personally use a while loop - not because it is better, but because it is just my preference. It is more "logical" imo. I have problems with the logical side of php, hehe - its easier if I can go through and read it out, like "while *something* do this, then increase $i then go back to the top and do *something* again..." etc! :D hehe
lol np ;) - was just letting him know incase he was getting confused. I always have a bit of trouble getting my logic right, I think pretty much everyone does :D
 
0
•••
u should see my desk before I start programming lol

there are loads of pieces of paper screwed up and thrown everywhere with brainstorms and mind maps and flow charts and stuff on them. I even do stuff like write out paragraphs, like a narrative, trying to get my head round what I am going to do! lol
 
0
•••
PoorDoggie said:
u should see my desk before I start programming lol

there are loads of pieces of paper screwed up and thrown everywhere with brainstorms and mind maps and flow charts and stuff on them. I even do stuff like write out paragraphs, like a narrative, trying to get my head round what I am going to do! lol
LOL,
you're much like me :P - Only I have heaps of problems when making my scripts, as I just dive straight into it lol, not a good way to go, but I'm learning new tricks each time ;).

Ok lol, we should give NetworkTown.Net his thread back now :D ;). Sorry to take it over NetworkTown.Net.
 
0
•••
Before we give NetworkTown his thread back:
I dive straight in, too. No planning FTW.
 
0
•••
Same here, Dan. I might draw out the DB if it;s got a large, confusing table two. But I generally don't write pseudo-code.

On a technical note, for loops are a bit quicker when executing as compared to while loops. It is also a lot easier to create infinite loops when using while.
 
0
•••
lol, to me, there's just no other way ;) I hate sitting there planning, I wish I would do it, but I don't lol, good work PoorDoggie! ;)
 
0
•••
LOL thanks all for the info it works ! :D
 
0
•••
Tree said:
It is also a lot easier to create infinite loops when using while.
hehe... don't I know it! :D
BillyConnite said:
lol, to me, there's just no other way ;) I hate sitting there planning, I wish I would do it, but I don't lol, good work PoorDoggie! ;)
I can't just dive in though. Its like a mental block, lol! :D :hehe: I need to be 100% clear on what I do, when I'm doing it! :D
 
0
•••
0
•••
Tree said:
I didn't say that... ;)
no, lol - I mean that I do it a lot! :D hehe... forget to put in the $i++ at the end, and end up crashing my server or computer or something. lol

Tom
 
0
•••
BillyConnite said:
LOL,
you're much like me :P - Only I have heaps of problems when making my scripts, as I just dive straight into it lol, not a good way to go, but I'm learning new tricks each time ;).

Ah the age-old SE methodology... 'code and fix' ;)

Andy
 
0
•••
andysyme said:
Ah the age-old SE methodology... 'code and fix' ;)

Andy

Cant beat the 'Dive right in without planning and then fix your way through a heap of bugs' method, lol
 
0
•••
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back