PDA

View Full Version : help with arrays -- how do they work and syntax.



mdcloud
04-30-2007, 04:41 PM
ok, i made a post last week about sorting on 2 varibles. i guess my code was too ugly to get a fix for so i went back to the drawing board. i think i know the concept i need, but do not know much coding.

i have a list of members. i need to sort them on date attending a class, and then be able to filter out of that list only the ones that are approved to come. would i need to use an array here to store the date? currently i use a while statment like this


while($u = mysql_fetch_array($users)) {
echo ' <table><tr><td>'.$u['FName'].'</td><td>'.$u['LName'].'</td></tr></table>';
}

This gets me my list. would i need to have those results go to an array and then do another while that pulled from that array? what is the process for that. i know i am asking alot, sorry i am new to this. i appreciate the help

mwinter
05-05-2007, 02:54 PM
i have a list of members. i need to sort them on date attending a class, and then be able to filter out of that list only the ones that are approved to come. would i need to use an array here to store the date?

That depends on what information is in the database. In principle, the DBMS itself can do all of this and return a sorted list of approved members.




while($u = mysql_fetch_array($users)) {
echo ' <table><tr><td>'.$u['FName'].'</td><td>'.$u['LName'].'</td></tr></table>';
}

A new table for every entry?

Mike

boogyman
05-05-2007, 09:46 PM
fix for that "new table every entry" is



echo "<table>";
while($u = mysql_fetch_array($users)) {
echo '<tr><td>'.$u['FName'].'</td><td>'.$u['LName'].'</td></tr>';
}
echo "</table>";

f
where you wrap the whole thing in a table.

mwinter
05-05-2007, 10:03 PM
fix for that "new table every entry" is

It may not need fixing; the posted code could be a simplification and a new table really is justified. That's why I just queried the code.





echo "<table>";
while($u = mysql_fetch_array($users)) {
echo '<tr><td>'.$u['FName'].'</td><td>'.$u['LName'].'</td></tr>';
}
echo "</table>";


As the query may also return and empty result set, it would be a good idea to check that there are actually results to write before including table start- and end-tags:



<?php
/* ... */
if (mysql_num_rows($users) > 0) {
?>
<table>
<?php
while (($row = mysql_fetch_array($users))) {
?>
<tr><td><?php echo $row['FName']; ?></td></tr>
<tr><td><?php echo $row['LName']; ?></td></tr>
<?php
}
?>
</table>
<?php
}
/* ... */
?>

Mike

GhettoT
05-06-2007, 09:00 PM
I know similar code has been posted ahead of me. But I have used this code for many projects, and it has not failed yet. So I will post if for anyone to use. Its pretty simple.



$server = "[SERVER]"; //url for the mysql server
$DBuser = "[USERNAME]"; //database username
$DBpass = "[PASSWORD]"; //database password
$DB = "[DATABASE NAME]"; //actual database name
$table = "[TABLE NAME]"; //database table name

mysql_connect($server,$DBuser,$DBpass); //connect to server
mysql_select_db($DB); //select database
$info = mysql_query("SELECT * FROM ".$table."");

//change the row names to fit your needs/wants
echo '<table>';
while ($qry = mysql_fetch_array($info)) {
echo '<tr><td>'.$qry[Lname].'</td><td>'.$qry[Fname].'</td></tr>;
}
echo '</table>';

It is important to have the periods "." after a ' or " and right before a ' or "
ie.

echo '<tr><td>'.$qry[Lname].'</td><td>'.$qry[Fname].'</td></tr>;