Jump to content

Archived

This topic is now archived and is closed to further replies.

Logix

While loop help

Recommended Posts

I am ready to bang my head o the wall on this..  I am doing a standard while loop with some tables.

[code]
$connect=mysql_connect("$db_Hostname","$db_UserName","$db_Password");
mysql_select_db("$db_Database",$connect);
$showpfsinst=mysql_query("SELECT * FROM Instructor WHERE Rank='CEI' ORDER BY LName ASC")or die;
while($pfsq = mysql_fetch_array($showpfsinst)) {


//START
Echo " <tr>\n";
Echo " <td align=middle><font face=Verdana size=1 color=" . $textcolor_inst . ">" . $pfsq[FName] . " " . $pfsq[LName] . "</font></a></td>\n";
Echo " <td align=middle><font face=Verdana size=1 color=" . $textcolor_inst . ">" . $pfsq[FName] . " " . $pfsq[LName] . "</font></a></td>\n";
Echo " <td align=middle><font face=Verdana size=1 color=" . $textcolor_inst . ">" . $pfsq[FName] . " " . $pfsq[LName] . "</font></a></td>\n";
Echo " <td align=middle><font face=Verdana size=1 color=" . $textcolor_inst . ">" . $pfsq[FName] . " " . $pfsq[LName] . "</font></a></td>\n";
Echo "</tr>\n";
//END
}
[/code]

This works - but displays the same record for each of the cells..  How can I change the record for each cell?  Basically I just want those 4 cells wide - each with a different record...

thank you in advance!!!!!!

Share this post


Link to post
Share on other sites
i think it will work try it
<?
while($result = @mysql_fetch_array($sel_query))
{
?>
    <tr>
      <td width="56%" colspan="2" class="nav_off3"><? print $result['sub_category'];?></td>
      <td width="22%"><a href="add_sub_category.php?id=<? print $result['id'];?>" class="nav_off1" target="_parent">Edit</a></td>
      <td width="22%"><a href="view_sub_category.php?del_id=<? print $result['id'];?>" class="nav_off1" onClick="javascript: return confirm('Are you Sure to DELETE <? print $result['sub_category'];?>');">Delete</a></td>
    </tr>
    <?
}
?>

Share this post


Link to post
Share on other sites
Use this instead:
[code=php:0]$c = 0;
while($pfsq = mysql_fetch_assoc($showpfsinst))
{
    echo ($c % 4) == 0) ? "<tr>\n" : '';

    echo ' <td align="middle"><font face="Verdana" size="1" color="' . $textcolor_inst . '>' . $pfsq[FName] . ' ' . $pfsq[LName] . "</font></a></td>\n";

    echo ($c % 4) == 0) ? "</tr>\n" : '';
}[/code]

Share this post


Link to post
Share on other sites
^ Nice wildteen. That is much nicer then the simple counter that I was going to post. I will use that from now on.

Share this post


Link to post
Share on other sites
Thank you!!!!!!

I will try it in a little bit and let you know....

Share this post


Link to post
Share on other sites
just a note.. your accessing your arrays incorrectly..

[code=php:0]$array['index'];[/code]

not

[code=php:0]$array[index];[/code]

Share this post


Link to post
Share on other sites
I am getting

Parse error: syntax error, unexpected ')', expecting ',' or ';' in /home/fighting/www/www/testdb/pfs_cei.php on line 54

until I change echo ($c % 4) == 0) ? "<tr>\n" : ''; to echo ($c % 4) == 0 ? "<tr>\n" : '';
but then I just get the same results i was without the count..

First time I am working with counters in code like this....


I change my array also - didnt make a difference tho but thanks

Share this post


Link to post
Share on other sites
I forgot to add $c++; to the end of the while loop so it wasnt counting!
[code=php:0]$c = 0;
while($pfsq = mysql_fetch_assoc($showpfsinst))
{
    // I missout the the opening ( on this line:
    echo (($c % 4) == 0) ? "<tr>\n" : '';

    echo ' <td align="middle"><font face="Verdana" size="1" color="' . $textcolor_inst . '>' . $pfsq['FName'] . ' ' . $pfsq['LName'] . "</font></a></td>\n";

    echo ($c % 4) == 0) ? "</tr>\n" : '';

    $c++;
}[/code]
Hows that now?

Share this post


Link to post
Share on other sites
I see how this works now..  Thank you...  Its almost there - I am getting weird output - but that might be formatting...  its doing

Name
Name  Name  Name
Name
Name  Name  Name

thanks alot man - at least I can learn from this code now...

Share this post


Link to post
Share on other sites
As I didnt test the code when I made it. It wasnt functioning correctly. try this new code:
[code=php:0]$c = 0;
while($pfsq = mysql_fetch_assoc($showpfsinst))
{
    echo (($c % 4) == 0) ? "  <tr>\n" : '';

    echo '    <td align="middle"><font face="Verdana" size="1" color="' . $textcolor_inst  . '">' . $pfsq['FName'] . ' ' . $pfsq['LName'] . "</font></td>\n";

    // If $c equal to 3 we reset the
    // counter and echo the closing table row tag
    if($c == 3) {
        echo "  </tr>\n";
        // reset counter to zero
        $c = 0;
        $tr = true;
    } else {
        // increment counter by 1
        $c++;
        $tr = false;
    }
}
// check that the the current table row hasn't been closed yet. If it hasn't we'll close it
echo ($c != 3 && $tr == false) ? "  </tr>\n" : '';[/code]

Share this post


Link to post
Share on other sites
WOW!!!!

I also got it to work by doing this

$c++;
Echo  ($c % 4) == 0 ? "</tr>\n" : ''

instead of

Echo  ($c % 4) == 0 ? "</tr>\n" : ''
$c++;

but now I am going to try your new way...  Thanks for everything I am learning a sh*t oad from this!

Share this post


Link to post
Share on other sites
Note I have update the code slightly as it wasnt closing the table rows correctly. Recopy the code posted above to replace the old code.

Share this post


Link to post
Share on other sites
this worked great - thanks a lot!!!!

My new problem (go figure) - is to list them the same way but up and down thenleft to right..

ie

currently:

a b c d
e f g h

I need to get
a c e g
b d f h


thanks for all your help!
b

Share this post


Link to post
Share on other sites
I've never done that before, However I believe it'll take a look of working out. I'll see if I can come up with something.

Share this post


Link to post
Share on other sites
yeah - I was trying to think how to do it - if its even possible.... ;D

Share this post


Link to post
Share on other sites

×

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.