Forum Moderators: coopster

Message Too Old, No Replies

The first record in my while is not showing when in two columns

while does not return first result when in two columns

         

iggabriel

2:10 pm on Mar 19, 2008 (gmt 0)

10+ Year Member



Hi everyone, I'm new to php so any help is appreciated with this one. The following code returns my mysql records in two columns like i want, but for the first one it only shows the image and does not populate the text from the database that should be beside it. So for the first record, it basically shows a blank cell, marking that it is there, then starts showing content on the second cell.

Thanks so much!

Code:

<?php

$cell_data = array();
$cell_data2 = array();

do
{

$cell_data[] = $row['name_first'];
$cell_data2[] = $row['name_last'];

}

while($row = mysql_fetch_array($attorneys_assoc));

$column_length = ceil(count($cell_data)/2);

$_2columns = "<table>";
for ($i = 0; $i < $column_length; $i++)

{
$_2columns .= "<tr><td><img src='images/spacer.gif' width='30' height='1'><img src='images/bullet.gif' width='7' height='7'>&nbsp;&nbsp;&nbsp;
<a href='attorney.php?name_first=".$cell_data[$i]."&name_last=".$cell_data2[$i]."'>".$cell_data[$i]." ".$cell_data2[$i]."</a></td><td><img src='images/spacer.gif' width='30' height='1'><img src='images/bullet.gif' width='7' height='7'>&nbsp;&nbsp;&nbsp;
<a href='attorney.php?name_first=".$cell_data[$i + $column_length]."&name_last=".$cell_data2[$i + $column_length]."'>".$cell_data[$i + $column_length]." ".$cell_data2[$i + $column_length]."</a></td></tr>";
}

$_2columns = $_2columns."</table>";
echo $_2columns;

?>

d40sithui

3:11 pm on Mar 19, 2008 (gmt 0)

10+ Year Member



hello,
i think it's because of your "do-while" loop. because it starts assigning things first before you use the mysql_fetch_array, the first array position is going to be empty..hence you're getting an empty row. you can check this actually if you manually do the query and compare the first record to the second cell (the one's that's displayed). two ways to fix this if it is the case.
(1)rather than a "do-while" just use while(mysql_fetch_array()) and then assign data inside it. (

while($row = mysql_fetch_array($attorneys_assoc)){
$cell_data[] = $row['name_first'];
$cell_data2[] = $row['name_last'];
}

2) or where you display your data, make an if/else statement that checks for empty contents.

if(!empty($cell_data[$i+column_length) && !empty($cell_data[$i+$column_length])){
//concatenate to $_2columns here
}
else{
//do nothing
}

iggabriel

6:30 pm on Mar 19, 2008 (gmt 0)

10+ Year Member



Hey. There's actually a record that's not showing up. The first record doesn't display. The first visible record is actually the second one in the result set.

mooger35

6:46 pm on Mar 19, 2008 (gmt 0)

10+ Year Member



Are you using mysql_fetch_array twice?

Perhaps right after your query?

iggabriel

10:13 pm on Mar 19, 2008 (gmt 0)

10+ Year Member



No, only once on the page. . . . Just to be clear, the db query returns "A, B, C, D" but the page shows " , B, C, D".

mooger35

10:41 pm on Mar 19, 2008 (gmt 0)

10+ Year Member



on your page use:

echo "<pre>";
print_r($cell_data);
print_r($cell_data2);
echo "</pre>";

does "A" show up in the array?

iggabriel

12:41 am on Mar 20, 2008 (gmt 0)

10+ Year Member



There was a problem with the code tying the query to the php. I'm so sorry. As I said, php is new to me. . . . The above php works fine so if anyone needs an example of a two-column display, it should work for you. . . . :)

iggabriel

11:26 am on Mar 20, 2008 (gmt 0)

10+ Year Member



One addtiional note for anyone reading this:

I found an exception in php4 that was causing an error. Please use this code if you are working in php4.

<?php

$cell_data = array();
$cell_data2 = array();

while($row = mysql_fetch_array($attorneys_assoc))
{

$cell_data[] = $row['name_first'];
$cell_data2[] = $row['name_last'];

}


$column_length = ceil(count($cell_data)/2);

$_2columns = "<table cellpadding='0' cellspacing='0'>";
for ($i = 0; $i < $column_length; $i++)

{

if (!isset($cell_data[$i + $column_length]))
{
$_2columns .= "<tr><td><img src='images/spacer.gif' width='30' height='1'><img src='images/bullet.gif' width='7' height='7'>&nbsp;&nbsp;&nbsp;
<a href='attorney.php?name_first=".$cell_data[$i]."&name_last=".$cell_data2[$i]."'>".$cell_data[$i]." ".$cell_data2[$i]."</a></td><td></td></tr>";
}
if (isset($cell_data[$i + $column_length]))
{
$_2columns .= "<tr><td><img src='images/spacer.gif' width='30' height='1'><img src='images/bullet.gif' width='7' height='7'>&nbsp;&nbsp;&nbsp;
<a href='attorney.php?name_first=".$cell_data[$i]."&name_last=".$cell_data2[$i]."'>".$cell_data[$i]." ".$cell_data2[$i]."</a></td><td><img src='images/spacer.gif' width='30' height='1'><img src='images/bullet.gif' width='7' height='7'>&nbsp;&nbsp;&nbsp;
<a href='attorney.php?name_first=".$cell_data[$i + $column_length]."&name_last=".$cell_data2[$i + $column_length]."'>".$cell_data[$i + $column_length]." ".$cell_data2[$i + $column_length]."</a></td></tr>";
}
}

$_2columns = $_2columns."</table>";
echo $_2columns;

?>