/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] For-loop, random number and table creation

Hey guys,

I am trying to create a random number between 1 and 10 for variable x in a PHP script.
Then whatever that random number is using for-loop the script should display this number in tables rows with the word “Hi!”

So lets say the random number is three the script should display three times the word “Hi!” in three table rows.

Any suggestion of how I can get this done?

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@NogDogDec 19.2009 — [code=php]
<table>
<?php
for($ix = 1, $total = rand(1,10); $ix <= $total; $ix++)
{
echo "<tr><td>Hi</td></tr>n";
}
?>
</table>
[/code]
Copy linkTweet thisAlerts:
@nMIK-3authorDec 19.2009 — [code=php]
<table>
<?php
for($ix = 1, $total = rand(1,10); $ix <= $total; $ix++)
{
echo "<tr><td>Hi</td></tr>n";
}
?>
</table>
[/code]
[/QUOTE]


NogDog thanks.

This is very close.

But the target is to display the words horizontally with the table border viewable. Can you provide this modification please?
Copy linkTweet thisAlerts:
@NogDogDec 19.2009 — NogDog thanks.

This is very close.

But the target is to display the words horizontally with the table border viewable. Can you provide this modification please?[/QUOTE]


Those are just HTML mark-up and CSS/style issues...surely you can figure that out? (I don't mean that to sound like a put-down, but I'd rather see you understand how the above code works than me just handing out free code to you.)
Copy linkTweet thisAlerts:
@nMIK-3authorDec 19.2009 — Those are just HTML mark-up and CSS/style issues...surely you can figure that out? (I don't mean that to sound like a put-down, but I'd rather see you understand how the above code works than me just handing out free code to you.)[/QUOTE]
NogDog, I completely understand your logic.

Before I ask I tried fixing it obviously. As always the point is to understand the process. This is why I am here..

I had no problem doing the table border but I cannot get this displayed in Rows and not columns... If you can point out what I am missing it would be great.
[CODE]
<table border="5">
<?php
for($ix = 1, $total = rand(1,10); $ix <= $total; $ix++)
{
echo "<tr><td>Hi</td></tr>";
}
?>
</table>[/CODE]
Copy linkTweet thisAlerts:
@NogDogDec 20.2009 — If you want them all on the same row, then the <tr> and </tr> tags would go outside of the loop (with the <table> and </table> tags), as you only want to repeat the <td>...</td> portion.
Copy linkTweet thisAlerts:
@nMIK-3authorDec 20.2009 — If you want them all on the same row, then the <tr> and </tr> tags would go outside of the loop (with the <table> and </table> tags), as you only want to repeat the <td>...</td> portion.[/QUOTE]
Thanks for the help NogDog that did it!

One last question if you can answer please.

At its current form, the script displays the word "Hi" in random times in only one table. How can I put every "Hi" at its own table?

For example if x=3 then three "Hi" displays in a three rows, each one of them at its own row?

I tried playing with the code but I had no luck doing it.

Once again thanks for everything.
Copy linkTweet thisAlerts:
@UltimaterDec 20.2009 — Hey thread. [i]waves and tries to catch up[/i].

Thanks for the help NogDog that did it![/quote]
Glad to hear you figured it out but you didn't show your code for the first scenario. So I know we are on "the same page", allow me to post a snippet in reply to for first post. But first lemme re-read your request:

I am trying to create a random number between 1 and 10 for variable x in a PHP script.

Then whatever that random number is using for-loop the script should display this number in tables [color=red]rows[/color] with the word "Hi!"
[/quote]

I think you meant to use the term "cells" rather than "rows". It happens to the best of us, "columns" and "rows" get confusing especially in the plural form. An easy way to remember which-is-which is that <TR> stands for Table Row while <TD> stands for Table Data better known as a "cell".

My code for your first case:
[code=php]
<table><tr><?php
$x = rand(1,10);
echo "$x is $xn";
for($i=0; $i<$x; $i++)echo '<td>Hi!</td>';
?></tr></table>
[/code]


Moving on.

*jumps to last post*

One last question if you can answer please.

At its current form, the script displays the word "Hi" in random times in only one table. How can I put every "Hi" at its own table?

For example if x=3 then three "Hi" displays in a three rows, each one of them at its own row?
[/quote]

If I understand you right (and you are still confusing rows and cells with each other) then here is a code snippet to do what you asked on a literal level:
[code=php]
<?php
$x = rand(1,10);
echo "$x is $xn";
for($i=0; $i<$x; $i++)
{
echo '<table><tr>';
for($j=0; $j<$x; $j++)echo '<td>Hi!</td>';
echo "</tr></table>n";
}
?>
[/code]

Here is the same code again on a more practical level re-written using TRs the way they were meant to be used rather than a noobish-looking series of TABLEs which would render in an ugly and repulsive matter if the cells were sized differently by unique text content.
[code=php]
<?php
$x = rand(1,10);
echo "$x is $xn";
?>
<table>
<?php
for($i=0; $i<$x; $i++)
{
echo '<tr>';
for($j=0; $j<$x; $j++)echo '<td>Hi!</td>';
echo "</tr>n";
}
?></table>
[/code]


And finally IN CASE you wanted the table to use TWO random numbers to control the rows and cells:
[code=php]
<?php
$x = rand(1,10);
$y = rand(1,10);
echo "$x is $x<br>$y is $yn";
?>
<table>
<?php
for($i=0; $i<$x; $i++)
{
echo '<tr>';
for($j=0; $j<$y; $j++)echo '<td>Hi!</td>';
echo "</tr>n";
}
?></table>
[/code]
Copy linkTweet thisAlerts:
@nMIK-3authorDec 20.2009 — Thank you so much for your very detailed reply.

I tried everything, very interesting info!

Here is the small problem I am still facing.

This is my code:
[CODE]<table border="5"><tr><td>
<?php
for($ix = 1, $total = rand(1,10); $ix <= $total; $ix++)

{
echo "Hi";
}
?>
</td></tr></table>[/CODE]


Everything is good with the only problem that the number of the words are all displayed together in one cell. (like attached pic 1). What I want to do is display the words in their own individual cells like attached pic 2.

Any idea of what is the problem?

[upl-file uuid=8feb4d9a-189b-4f39-a135-809ee7b06be6 size=9kB]pic1.JPG[/upl-file]

[upl-file uuid=6e25b47d-0174-4b4c-9b25-b285c56bc093 size=10kB]pic2.JPG[/upl-file]
Copy linkTweet thisAlerts:
@UltimaterDec 20.2009 — The first code snippet in my previous post does this other than the border and $i counts the first loop as 0 rather than 1 but if you want to stick to NogDog's coding-style it would be:
[code=php]
<table border="5"><tr>
<?php
for($ix = 1, $total = rand(1,10); $ix <= $total; $ix++)

{
echo "<td>Hi</td>";
}
?>
</tr></table>
[/code]
Copy linkTweet thisAlerts:
@nMIK-3authorDec 20.2009 — I got it now!

Thanks a lot for the clarification Ultimater, I really appreciate your help.

Problem solved ?
×

Success!

Help @nMIK-3 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...