/    Sign up×
Community /Pin to ProfileBookmark

Problems Using WHILE loops to fill a Table

Hey all,

I’m VERY new to PHP but I’ve managed to create a large part of my first program. I’m stuck on this one part which is preventing me from moving on.

I’m trying to populate a table with information from mysql. I’ve gotten the first column completed, but I can’t seem to get the 2nd column populated properly. There’s always repeating data from either column 1 or column 2.

Here’s my code…

[code=php]
if ($result)
{
while ($usa_carriers_row = mysql_fetch_array($usa_carriers_result)) {

while ($usa_prepaid_row = mysql_fetch_array($usa_prepaid_result)) {

echo “<tr><td>”;
echo $usa_carriers_row[0];
echo “</td><td>” ;
echo $usa_prepaid_row [0];
echo “</td></tr>” ;

}
}
}

echo “</table>”;

[/code]

I’ve left out the html code for setting up the table columns etc. Does anyone know how to do this properly with loops? This is what it looks like right now.

COL #1 COL #2
Carrier#1 0.1
Carrier#1 0.5

What it should say is….

COL #1 COL #2
Carrier #1 0.1
Carrier #2 0.5

There’s about 6 columns that I need to use from my database. Any help is GREATLY appreciated!

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@tirnaMar 30.2010 — Hopefully this skeleton template will help you debug the problem you have.

[code=php]
<?php $rs = mysql_query($query,$conn) ?>

<table...............>

<?php

//say you have 3 columns in the result set
while($row=mysql_fetch_assoc($rs)) {
echo '<tr>';
echo '<td>'.$row['col1Name'].'</td>';
echo '<td>'.$row['col2Name'].'</td>';
echo '<td>'.$row['col3Name'].'</td>';
echo '</tr>';
}
?>

</table>

[/code]
Copy linkTweet thisAlerts:
@Jarrod1937Mar 30.2010 — First off, if you can combine the two datasets into the same query, that'll make it to where you only need one loop. It will also ensure that the two columns datasets match. Currently with two different datasets, any gap in one will cause a mismatch. If you can't for whatever reason and both datasets have an equal number of results you should run them together like this:

[code=php]
if ($result)
{
while ($usa_prepaid_row = mysql_fetch_array($usa_prepaid_result) && $usa_carriers_row = mysql_fetch_array($usa_carriers_result)) {

echo "<tr><td>";
echo $usa_carriers_row[0];
echo "</td><td>" ;
echo $usa_prepaid_row [0];
echo "</td></tr>" ;

}
}

echo "</table>";
[/code]


You should also use an associative array for the printing of the results. The numerical index value can change depending on the order and the amount of fields you're returning, using an associative array removes the need to worry about that.
Copy linkTweet thisAlerts:
@smohauthorMar 30.2010 — Thanks for the suggestion, but I don't think it will work. I should have explained in my first post that my queries are specific to columns depending on certain WHERE conditions. For example

[code=php]
$q1 = "SELECT carrier FROM carriers WHERE country='USA' AND technology='gsm' ";
$r1 = mysql_query($q1);


[/code]



So wouldn't this not work since I can't grab the whole column?
Copy linkTweet thisAlerts:
@tirnaMar 30.2010 — Unless I am misunderstnding you, it shouldn't make any difference.

For example:

Say your db table has the following column names.

[CODE]fldUserID, fldFirstName, fldSecondName, fldAddress, fldAccessLevel[/CODE]

and my sql query is:

[CODE]$query = select fldUserID,fldFirstName,fldSecondName from tbluser where fldAccessLevel = 1;[/CODE]

$rs = mysql_query($query);

$rs would contain the returned rows for column names fldUserID, fldFirstName, fldSecondName

and my php code to output the returned rows into a table cell would be

[code=php]
<?php $rs = mysql_query($query,$conn) ?>
<table...............>
<?php
//say you have 3 columns in the result set

while($row=mysql_fetch_assoc($rs)) {
echo '<tr>';
echo '<td>'.$row['fldUserID'].'</td>';
echo '<td>'.$row['fldFirstName'].'</td>';
echo '<td>'.$row['fldSecondName'].'</td>';
echo '</tr>';
}
?>
</table>
[/code]
Copy linkTweet thisAlerts:
@smohauthorMar 30.2010 — Tirna:

Your solution works perfect. Thank you so much for your help!


Jarrod1937:

I tried your solution and it didn't work for me. Maybe I had something wrong. Thank you for your help though!


SIDE NOTE:

Is it possible to pull call a variable set in a previous IF statement? I think they're called static variables? Don't quote me on that.
Copy linkTweet thisAlerts:
@tirnaMar 30.2010 — no problem - I'm glad you sorted it out ?
×

Success!

Help @smoh 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.19,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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