Jump to content

[SOLVED] how to do it (foreach)


d22552000

Recommended Posts

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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />';
}

Link to comment
Share on other sites

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?
Link to comment
Share on other sites

// 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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.. :P

Apparently it's not as good as the other one even though the other one doesn't output anything..

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.