isbhenrylu Posted August 16, 2009 Share Posted August 16, 2009 Hi guys, I've got a problem to do with the while() function. What I'm trying to do is to create a table of 2 columns, one column is user id and the other is user names. This is what I did: <table border="2px"> <tr> <th>NameId</th> <th>Name</th> </tr> <?php $c = 1; define ('NAME1', 'Henry'); define ('NAME2', 'Mary'); define ('NAME3', 'Gary'); define ('NAME4', 'Cherry'); define ('NAME5', 'Berry'); $i = 1; while ($c <= 5){ echo "<tr><td>" . $c . "</td>"; while ($i == $c){ echo "<td>" . NAME . $i . "</td>"; $i++; } echo "</td>"; $c++; } ?> </table> The user id part works fine, going from 1-5 for the 5 rows. But for the user name column, the result is NAME1, NAME2, NAME3, NAME4, NAME5 instead of Henry, Mary, Gary, Cherry, Berry. Can someone please tell me why echo "<td>" . NAME . $i . "</td>"; is interpreted literally as NAME1 instead of finding the constant NAME1 and returning the value "Henry", which was defined in the constant at the start of the script. Really appreciate if someone can show me how to fix this problem as I'm a newbie in programming, thanks! Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/ Share on other sites More sharing options...
DEVILofDARKNESS Posted August 16, 2009 Share Posted August 16, 2009 it would be easier to say while ($c <= 5){ echo "<tr><td>" . $c . "</td><td>" . NAME . $i . "</td>"; $i++; $c++; } but please use code-tags Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/#findComment-899409 Share on other sites More sharing options...
isbhenrylu Posted August 17, 2009 Author Share Posted August 17, 2009 Sorry mate, but this still doesn't solve the problem. The results is still: NameId Name 1 NAME1 2 NAME2 3 NAME3 4 NAME4 5 NAME5 instead of what I want it to yield, which is suppose to be: NameId Name 1 Henry 2 Mary 3 Gary 4 Cherry 5 Berry Please help! thanks Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/#findComment-899981 Share on other sites More sharing options...
DEVILofDARKNESS Posted August 17, 2009 Share Posted August 17, 2009 yeah ofcourse it does. maybe this helps: while ($c <= 5){ echo "<tr><td>" . $c . "</td><td>'NAME" . $i . "'</td>"; $i++; $c++; } Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/#findComment-899991 Share on other sites More sharing options...
trq Posted August 17, 2009 Share Posted August 17, 2009 Your code won't work straight up with simple variables let alone constants. You might try this.... echo "<td>{NAME . $i}</td>"; But I'm not sure it'll work and I can't test atm. Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/#findComment-899993 Share on other sites More sharing options...
trq Posted August 17, 2009 Share Posted August 17, 2009 Yeah, actually thats not going to work. constants aren't interpolated within strings. Really don't see any way of doing what you want (variable variables with constants. Variables, yeah, simple. Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/#findComment-899995 Share on other sites More sharing options...
Mark Baker Posted August 17, 2009 Share Posted August 17, 2009 echo "<td>".constant('NAME'.$i)."</td>"; Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/#findComment-900018 Share on other sites More sharing options...
DEVILofDARKNESS Posted August 17, 2009 Share Posted August 17, 2009 that should work, to call a define(...) you need to use echo constant(...) I just saw it on w3schools but baker was faster Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/#findComment-900021 Share on other sites More sharing options...
isbhenrylu Posted August 18, 2009 Author Share Posted August 18, 2009 Thanks it worked. Really appreciate it. Link to comment https://forums.phpfreaks.com/topic/170506-solved-problem-with-the-while-function/#findComment-900677 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.