/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Receiving error – please help

Hi,

Firstly the following code accomplishes the task that I require, although there is an error message that appears if there is no data in the database to populate the last data cell in the second column.

PHP:

[code=php]<table class=”main”>
<tr valign=”top”>
<td class=”maincell”>
<?php
$sql = ‘SELECT name, age, height FROM test’;

$result = mysql_query($sql) or die(mysql_error());

$iniLoop = 0;
$endLoop = mysql_num_rows($result);

while ( $iniLoop < $endLoop ) {
?>
<table class=”list”>
<tr valign=”top”>
<td class=”columnone”>
<span class=”image”>
<img src=”image.jpg” />
</span>
<span class=”data”>
<?php
echo mysql_result($result, $iniLoop, ‘name’);
print “<br>”;
echo mysql_result($result, $iniLoop, ‘age’);
print “<br>”;
echo mysql_result($result, $iniLoop, ‘height’);
?>
</span>
</td>
<td class=”columntwo”>
<span class=”image”>
<img src=”image.jpg” />
</span>
<span class=”data”>
<?php
echo mysql_result($result, $iniLoop+1, ‘name’);
print “<br>”;
echo mysql_result($result, $iniLoop+1, ‘age’);
print “<br>”;
echo mysql_result($result, $iniLoop+1, ‘height’);
?>
</span>
</td>
</tr>
</table>
<?php
$iniLoop += 2;

if ($endLoop <= $iniLoop+1) {
echo “<td></td>”;
}
}
?>
</td>
</tr>
</table>[/code]

The error message appears in a scenario such as this:

POPUPLATED || POPUPLATED
=======================================================

POPUPLATED || NOT POPUPLATED – ERROR MESSAGE APPEARS

The error message is:

[QUOTE]

Warning: mysql_result() [function.mysql-result]: Unable to jump to row 3 on MySQL result index 2 in /home/*****/public_html/test/testdisplay.php on line 134

[/QUOTE]

Is anyone able to help with this issue ?

Many thanks in advance.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@MindzaiApr 27.2009 — [code=php]
$sql = 'SELECT name, age, height FROM test';
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "Name: {$row['name']} Age: {$row['age']} Height: {$row['height']}n"
}[/code]
Copy linkTweet thisAlerts:
@osirisjackoauthorApr 27.2009 — [code=php]
$sql = 'SELECT name, age, height FROM test';
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
echo "Name: {$row['name']} Age: {$row['age']} Height: {$row['height']}n"
}[/code]
[/QUOTE]

Hey, thanks for your reply.

Unfortunately the code you've supplied isn't what I am trying to achieve.

There is 2 columns displaying a row of data each, when those two columns are populated the table goes onto another row and repeats again with different data from the database.

Attached is a screen shot of the issue I am experiencing...

As you can see on the 2nd row, 2nd column, the error is displaying as there isn't another row in the database to populate data. I need a way around this so it doesn't display an error message like shown - perhaps a solution like print a blank <td></td> within the source if no other data available to populate, although I am not sure how to do this...

[upl-file uuid=8ff0fb7d-3bd3-45fe-9571-e6961bffece7 size=25kB]issue.jpg[/upl-file]
Copy linkTweet thisAlerts:
@MindzaiApr 27.2009 — If you use the above method to loop your result set you won't get the error. You will need to modify it to make it work in your situation, but I would personally recommend using css instead of tables for layout. Floating divs into columns is a much neater solution and will fit nicer with this approach too as you dont need to worry about odd numbers of results and creating empty table cells etc.
Copy linkTweet thisAlerts:
@osirisjackoauthorApr 27.2009 — If you use the above method to loop your result set you won't get the error. You will need to modify it to make it work in your situation, but I would personally recommend using css instead of tables for layout. Floating divs into columns is a much neater solution and will fit nicer with this approach too as you dont need to worry about odd numbers of results and creating empty table cells etc.[/QUOTE]
True statement. I'll have a go with CSS instead of tables to display the data. What I need for this looping display is two containers (columns) which have two "columns" within that as well. Thanks for putting this forward to me for consideration.
Copy linkTweet thisAlerts:
@osirisjackoauthorApr 27.2009 — True statement. I'll have a go with CSS instead of tables to display the data. What I need for this looping display is two containers (columns) which have two "columns" within that as well. Thanks for putting this forward to me for consideration.[/QUOTE]
I've changed it to use CSS instead of tables, [B]however[/B], how will I be able to rotate the div id class on the two columns (containers) using your method of loop above?
Copy linkTweet thisAlerts:
@MindzaiApr 27.2009 — To toggle something the best way is some plain old number crunching:

<i>
</i>$i = 0;
while ($row = mysql_fetch_array($result)) {
echo ($i &amp;#37; 2 == 0) ? 'class 1' : 'class 2';
$i++;
}


(sorry for lack of highlighting but it seems the php bb tag can't cope with the modulo operator.)
Copy linkTweet thisAlerts:
@osirisjackoauthorApr 28.2009 — Was able to modify with help from other forum with my orginal code which has now sorted the problem.

Issue resolved:

[code=php]<div class="align_products">

<table class="main">
<tr valign="top">
<td class="maincell">
<?PHP
$sql = 'SELECT profileimage, name, age, height FROM test';

$result = mysql_query($sql) or die(mysql_error());

$userData = array();

while ( $row = mysql_fetch_array($result) ) $userData[] = $row;

$iniLoop = 0;
$endLoop = sizeof($userData);

while ( $iniLoop < $endLoop ) {

if ( $userData[$iniLoop+1] ) {

$column2 = $userData[$iniLoop+1];
$col2Class = 'class="columntwo"';
}
else {

$column2 = array('','','','');
$col2Class = '';
}
?>
<table class="list">
<tr valign="top">
<td class="columnone">
<span class="image">
<?PHP echo $userData[$iniLoop][0]; ?>
</span>
<span class="data">
<?PHP echo $userData[$iniLoop][1]; ?>
<br>
<?PHP echo $userData[$iniLoop][2]; ?>
<br>
<?PHP echo $userData[$iniLoop][3]; ?>
</span>
</td>
<td <?PHP echo $col2Class; ?>>
<span class="image">
<?PHP echo $column2[0]; ?>
</span>
<span class="data">
<?PHP echo $column2[1]; ?>
<br>
<?PHP echo $column2[2]; ?>
<br>
<?PHP echo $column2[3]; ?>
</span>
</td>
</tr>
</table>
<?PHP
$iniLoop += 2;
}
?>
</td>
</tr>
</table>

</div>
[/code]
Copy linkTweet thisAlerts:
@MindzaiApr 28.2009 — You're opening yourself up to quite a few notice errors due to undefined indexes there.
×

Success!

Help @osirisjacko 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.17,
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,
)...