d22552000 Posted September 3, 2007 Share Posted September 3, 2007 foreach($row['STDNTID'] as $stdntid) { as you guessed it, I am querying my databse. my database lists students in a class by STDNTID## so how do I do a foreach that does it like for each variable STDNTID## ? foreach($row['STDNTID'] as $stdntid) {} returns nothing. as $stdntid foreach($row['STDNTID##'] as $stdntid) {} returns a few errors, ugh. So, how do I do it? is there a way to generate an array with the names of clsid1-35 ? IDK help! Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 Try foreach(mysql_fetch_array($result) as $row)echo$row['STDNTID']; Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 cant, im already getting the rows with a while while($row = mysql_fetch_array($res, MYSQL_ASSOC)) { } Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 What is the foreach for then? The individual keys returned from that while loop aren't going to be having subarrays in them are they? Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 ok my table is COLUMNS: clsid stdntid1 stdntid2 AND SO ON. wihtout writing out one thing for EACH student column and typing int he individual names (stdntid1...) I want to do a foreach like foreach variable? $row['stdntid1'] $row['stdntid2'] and Im not lazy its just that the number of students can change. and the db can easily get extra columns added. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 3, 2007 Share Posted September 3, 2007 foreach can only be used if the variable you pass it contains an array. foreach wont loop through similar variables. what you'll want to do is count how many items $row contains, substract the total items by 1 then using a for loop echo out $row['stdntidX'] example: // get total items in the $row array $items = count($row) - 1; // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= $items; $i++) { echo $row['stdntid' . $i] . '<br />'; } Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 ok my table is COLUMNS: clsid stdntid1 stdntid2 AND SO ON. wihtout writing out one thing for EACH student column and typing int he individual names (stdntid1...) I want to do a foreach like foreach variable? $row['stdntid1'] $row['stdntid2'] and Im not lazy its just that the number of students can change. and the db can easily get extra columns added. Your while statement is fetching them. Why not just echo them in it? Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 // get total items in the $row array $items = count($row) - 1; // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= $items; $i++) { echo $row['stdntid' . $i] . '<br />'; } im trying that out. uh no work BLANK page wher the thing should be while($row = mysql_fetch_array($res, MYSQL_ASSOC)) { // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= 30; $i++) { echo $row['stdntid' . $i] . '<br />'; } } I also tried that with no luck either. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 Oh I think I see what you're trying to do now. Maybe this will work? foreach($row as $a)echo$a; Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 nope that dont work... cause it just outputs blank how sad: http://www.wftv.com/news/6253589/detail.html Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 3, 2007 Share Posted September 3, 2007 nope that dont work... cause it just outputs blank its got to be outputing something! If the following doesn't output any thing: while($row = mysql_fetch_array($res, MYSQL_ASSOC)) { echo 'row contents: ' . print_r($row); } Then the above block of code is not be ran, due to an error or a logic error in your code. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 I think, IM AN IDIOT. My database (for testing purposes) was using classid 1 and my forum started at 101. omfg im an idiot. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 lol now while($row = mysql_fetch_array($res, MYSQL_ASSOC)) { // get total items in the $row array $items = count($row) - 1; // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= $items; $i++) { echo $row['stdntid' . $i] . '<br />'; } } outputs 30 BR and no values. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 I'm not sure about the code you just posted, but the code I posted WILL echo all columns in the array.. I've tested it myself and it works fine. If it's not outputting anything for you, then it's because your query is broken and is not retrieving anything to be outputted, or your while loop is somehow broken and isn't retrieving the rows. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 yours does work but it doesnt ignore rows NOT names clsid like his code does. the only problem whith his code is its not outputting the values. Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 3, 2007 Share Posted September 3, 2007 How is $row formatted? Run print_r on $row in your while loop to see how $row is formatted. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted September 3, 2007 Share Posted September 3, 2007 foreach can only be used if the variable you pass it contains an array. foreach wont loop through similar variables. what you'll want to do is count how many items $row contains, substract the total items by 1 then using a for loop echo out $row['stdntidX'] example: // get total items in the $row array $items = count($row) - 1; // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= $items; $i++) { echo $row['stdntid' . $i] . '<br />'; } Why wouldn't you be able to do this? o_O <?php foreach($row as $r) { echo $r.'<br />'; } ?> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 3, 2007 Share Posted September 3, 2007 foreach can only be used if the variable you pass it contains an array. foreach wont loop through similar variables. what you'll want to do is count how many items $row contains, substract the total items by 1 then using a for loop echo out $row['stdntidX'] example: // get total items in the $row array $items = count($row) - 1; // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= $items; $i++) { echo $row['stdntid' . $i] . '<br />'; } Why wouldn't you be able to do this? o_O <?php foreach($row as $r) { echo $r.'<br />'; } ?> No no, misunderstood me there, before they was passing $row['stdntid1'] and not $row in the foreach, thinking it will loop through $row['stdntid1'], $row['stdntid2'] $row['stdntid3'] etc when using $row['stdntid1']. Thats what i meant by : foreach can only be used if the variable you pass it contains an array. foreach wont loop through similar variables. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 beacsue i have some rows that arent called clsid.. and I ONLY want the ones called clsid. $ROW is formatted as my database is...: TEAID and CLSID 1 through 59 followed by TEAPW and TEAEP TEAIQ TEAVP and TEAND Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 foreach can only be used if the variable you pass it contains an array. foreach wont loop through similar variables. what you'll want to do is count how many items $row contains, substract the total items by 1 then using a for loop echo out $row['stdntidX'] example: // get total items in the $row array $items = count($row) - 1; // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= $items; $i++) { echo $row['stdntid' . $i] . '<br />'; } Why wouldn't you be able to do this? o_O <?php foreach($row as $r) { echo $r.'<br />'; } ?> That's basically what I put, on page 1.. Apparently it's not as good as the other one even though the other one doesn't output anything.. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 but it doestnn wokr example: // get total items in the $row array $items = count($row) - 1; // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= $items; $i++) { echo $row['stdntid' . $i] . '<br />'; } just ouptuts 59 <br /> without the values. I WANT THE VALUES/ Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 3, 2007 Share Posted September 3, 2007 OMG! Everyone please read my post here. I know you can use foreach to loop through $row, but you can't usit it loop through simiar variables. That is how d22552000 was using foreach earlier. d22552000 hold on a sec. Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Author Share Posted September 3, 2007 yay SOMEONE is listening Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted September 3, 2007 Share Posted September 3, 2007 Ok lets continue. beacsue i have some rows that arent called clsid.. and I ONLY want the ones called clsid. $ROW is formatted as my database is...: TEAID and CLSID 1 through 59 followed by TEAPW and TEAEP TEAIQ TEAVP and TEAND Where does stdntid1, stdntid2, stdntid3 etc come into it? Or is stdntid supposed to be clsid? Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 but it doestnn wokr example: // get total items in the $row array $items = count($row) - 1; // now that we not how many items are in the $row array, // we can use a for loop to loop through the array for($i = 1; $i <= $items; $i++) { echo $row['stdntid' . $i] . '<br />'; } just ouptuts 59 <br /> without the values. I WANT THE VALUES/ Yes I know his doesn't work. You already said that lol. Anyways, an easy way would probably just be to concatenate your results in your mysql query, so that all of those ones that you want to loop through, are returned as a single row, separated by commas or something. Then just use explode and BANG there you go. 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.