Jragon Posted July 20, 2010 Share Posted July 20, 2010 <html> <head> <title>Hats</title> </head> <body> <?php /* Rules: 1 hat must be worn all day A hat must be worn every weekday Cant change hat once put on in the moring Mathmatical Statment 4*4*4*4*4 = 1024 What is going to happen: Every statment will be echoed out E.G: <table> <tr> <th>Monday</th> <th>Tuseday</th> <th>Wesnesday</th> <th>Thesday</th> <th>Friday</th> </tr><tr> <td>Pink</td> <td>Brown</td> <td>Blue</td> <td>Red</td> <td>Red</td> </tr> </table> */ $hat1 = 'Pink'; $hat2 = 'Brown'; $hat3 = 'Blue'; $hat4 = 'Red'; $odd = true; while ($num <= 1024){ } ?> </body> </html> If you reed the notes it says what i want to do If anyone has any ideas how i can do this it would be cool Thanks Jragon Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 If you reed the notes it says what i want to do No it doesn't or it does in a very cryptic way. Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 It says what i want to happen Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 The point is it doesn't say how the variables you have are related to do output you want. The simplest way is just to echo what you want. echo "<table> <tr> <th>Monday</th> <th>Tuseday</th> <th>Wesnesday</th> <th>Thesday</th> <th>Friday</th> </tr><tr> <td>Pink</td> <td>Brown</td> <td>Blue</td> <td>Red</td> <td>Red</td> </tr> </table>"; but I suspect it's not what you're after is it? Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 No. Because I'm lasy and dont want to make a table my self for each differnt time. I want php to do it for me using somthing like a while loop or a for loop in the end it would make somthing like this: <table> <tr> <th>Monday</th> <th>Tuseday</th> <th>Wesnesday</th> <th>Thesday</th> <th>Friday</th> </tr><tr> <td>Pink</td> <td>Brown</td> <td>Blue</td> <td>Red</td> <td>Red</td> </tr><tr> <td>pink</td> <td>pink</td> <td>pink</td> <td>pink</td> <td>pink</td> </tr> <tr> <td>blue</td> <td>blue</td> <td>blue</td> <td>blue</td> <td>blue</td> </table> ect. But i have no idea how to do this Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 Well I have no idea what you want. Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 It is going to make a table with a differnt color hat worn every day. Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 You mean permutations? Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 sort ov. the first paragraph is almost correct eddited verstion: In mathematics, the notion of permutation is used with several slightly different meanings, all related to the act of permuting (rearranging in an ordered fashion) objects or values. Informally, a permutation of a set of values is an arrangement of those values into a particular order. Thus there are six permutations of the set {1,2,3}, namely [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1], [3,3,3], [2,2,2], [1,1,1], [3,3,2], [3,2,3] ECT. Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 Permutations with repetitions then. The crude way looks like this. $hats = array('pink','brown','blue','red'); foreach($hats as $hat1) { foreach($hats as $hat2) { foreach($hats as $hat3) { foreach($hats as $hat4) { foreach($hats as $hat5) { echo $hat1." ".$hat2." ".$hat3." ".$hat4." ".$hat5.PHP_EOL; } } } } } Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 How should i incorperate that in to a table that has the days of the week as headings and numbers down the side? Also how dose this work? Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 Come on... is it that difficult to add <tr> and <td> tags to this echo? And as far as foreach goes... here's manual: foreach I've created a recursive function in the meantime, but I'm afraid of even showing it to you... Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 Why would you be afraid of showing me? Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 Also when i am trying to add numbers to it i get this error Parse error: syntax error, unexpected $end in C:\xampp\htdocs\mathstatment.php on line 56 my code: $hats = array('pink','brown','blue','red'); $i = 1; foreach($hats as $hat1) { foreach($hats as $hat2) { foreach($hats as $hat3) { foreach($hats as $hat4) { foreach($hats as $hat5) { echo $i . $hat1 . " " . $hat2 . " " . $hat3 . " " . $hat4 . " " . $hat5 . PHP_EOL . '<br />'; $i++; } } } } Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 That's exactly why. Count the braces. Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 fixed it =] Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 where should i put the PHP_eol in my table? also what dose that function do(i cant find it on the manual) Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 I gave you a direct link to foreach manual page... how can you not find it? PHP_EOL is a line break. I use it because I ran this script in console. For HTML you should use <br/> Here's my recursive function if you're interested: <?php function permute($array,$positions,&$perms,$previous = null) { if($positions > 0) { if(!isset($previous)) { $previous = array(); } foreach($array as $element) { $tempPrevious = $previous; $tempPrevious[] = $element; permute($array,$positions-1,$perms,$tempPrevious); } } else { $perms[] = $previous; } } $perms = array(); $hats = array('pink','brown','blue','red'); $days = 5; permute($hats, $days, $perms); echo "<table>"; foreach($perms as $key => $row) { echo "<tr><td>".($key+1)."</td>"; foreach($row as $hat) { echo "<td>$hat</td>"; } echo "</tr>".PHP_EOL; } echo "</table>"; Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 This is mine <html> <head> <title>Hats</title> <style type="text/css"> th { background-color: #999;} .odd_row { background-color: #EEE; } .even_row { background-color: #FFF; } </style> </head> <body> <table border="0"> <tr> <th>Number</th> <th>Monday</th> <th>Tuseday</th> <th>Wesnesday</th> <th>Thesday</th> <th>Friday</th> </tr> <?php /* Rules: 1 hat must be worn all day A hat must be worn every weekday Cant change hat once put on in the moring Mathmatical Statment 4*4*4*4*4 = 1024 What is going to happen: Every statment will be echoed out E.G: <table> <tr> <th>Monday</th> <th>Tuseday</th> <th>Wesnesday</th> <th>Thesday</th> <th>Friday</th> </tr><tr> <td>Pink</td> <td>Brown</td> <td>Blue</td> <td>Red</td> <td>Red</td> </tr> </table> */ $hats = array('pink','brown','blue','red'); $i = 1; foreach($hats as $hat1) { foreach($hats as $hat2) { foreach($hats as $hat3) { foreach($hats as $hat4) { foreach($hats as $hat5) { echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">'; echo <<<ENDHTML <td>{$i}</td> <td>{$hat1}</td> <td>{$hat2}</td> <td>{$hat3}</td> <td>{$hat4}</td> <td>{$hat5}</td> </tr> ENDHTML; $odd = !$odd; $i++; } } } } } ?> </table> </body> </html> Thanks for the help Jragon Quote Link to comment Share on other sites More sharing options...
Mchl Posted July 20, 2010 Share Posted July 20, 2010 For ultimate fun do echo <<<ENDHTML <td>{$i}</td> <td style="background: {$hat1};">{$hat1}</td> <td style="background: {$hat2};">{$hat2}</td> <td style="background: {$hat3};">{$hat3}</td> <td style="background: {$hat4};">{$hat4}</td> <td style="background: {$hat5};">{$hat5}</td> </tr> ENDHTML; Quote Link to comment Share on other sites More sharing options...
Jragon Posted July 20, 2010 Author Share Posted July 20, 2010 Thats cool! is there anyway to change the text color cos its a little hard to read Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.