Jump to content


Photo

While loop help


  • Please log in to reply
14 replies to this topic

#1 Logix

Logix
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationDirty Jersey

Posted 01 September 2006 - 12:30 PM

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

$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
}

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!!!!!!

#2 rajmohan

rajmohan
  • Members
  • PipPipPip
  • Advanced Member
  • 169 posts

Posted 01 September 2006 - 12:36 PM

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>
    <?
}
?>

#3 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 01 September 2006 - 12:40 PM

Use this instead:
$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" : '';
}


#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 01 September 2006 - 12:41 PM

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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 Logix

Logix
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationDirty Jersey

Posted 04 September 2006 - 10:36 AM

Thank you!!!!!!

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

#6 Jenk

Jenk
  • Members
  • PipPipPip
  • Advanced Member
  • 778 posts

Posted 04 September 2006 - 11:45 AM

just a note.. your accessing your arrays incorrectly..

$array['index'];

not

$array[index];


#7 Logix

Logix
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationDirty Jersey

Posted 04 September 2006 - 11:47 AM

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



#8 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 11:56 AM

I forgot to add $c++; to the end of the while loop so it wasnt counting!
$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++;
}
Hows that now?

#9 Logix

Logix
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationDirty Jersey

Posted 04 September 2006 - 12:28 PM

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

#10 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 01:00 PM

As I didnt test the code when I made it. It wasnt functioning correctly. try this new code:
$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" : '';


#11 Logix

Logix
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationDirty Jersey

Posted 04 September 2006 - 01:05 PM

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!

#12 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 01:13 PM

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.

#13 Logix

Logix
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationDirty Jersey

Posted 04 September 2006 - 09:00 PM

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

#14 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 04 September 2006 - 10:24 PM

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.

#15 Logix

Logix
  • New Members
  • Pip
  • Newbie
  • 8 posts
  • LocationDirty Jersey

Posted 04 September 2006 - 10:55 PM

yeah - I was trying to think how to do it - if its even possible.... ;D




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users