<?php while ($row = mysql_fetch_assoc($result)) {?>
<table width="528" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
<tr>
<td bgcolor="#FFFFFF"><strong>Headline:<?php echo $row['headlinex'];?></strong></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">Main Story:<?php echo $row['storyx'];?></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">Posted on <?php echo $row['datex']?> by <?php echo $row['posterx'];?></td>
</tr>
</table>
<?php }?>
Now let us analyse whats going on in this little code.
<?php
while ($row = mysql_fetch_assoc($result)) {?>
as u already know, we need an open and close tag that tells apache, this is a php business, hand it over to him
and that open and close tag is <?php some_codes_here ?>
lets leave while out for a second, lets digest this first
$row = mysql_fetch_assoc($result)
$row is a variable we just created, i mean it does not exist in any of php's standard, we cud have called it $anything
so we are saying mysql_fetch_assoc($result) is equal to our newly created $row
like i said earlier, anything in this "example()" format is called function in PHP, it does perform a function and some function requires that you supply a parameter while others dont
e.g. exit() does a job of terminating any script, -requires not parameter
unlink($parameter) does the job of deleting any file from any directory as long as the file permits it, -requires a parameter which must be a file name
mysql_connect($server, $username, $password, $optional1, $optional2) help connect to mysql database -requires 3 compulsory parameters and 2 optional parameters
so our mysql_fetch_assoc($result) is just a function that fetches the result from our previous query (our query was assigned to $result, remember!)
if you look at it closely, mysql_fetch_assoc() function is not smart enough to read our script to determine what he needs to act on, we had to give him the result so he could do his job on it
it's job is to fetch the result and make them available as an associative array, (not as hard as it sounds)
What is an array
An array is an ordered map,
e.g. Color=>yellow
Forum => nairaland
Forum => (if u cant find me on nairaland, you will find me here)
Cars => toyota camry 2004 model (i have this for sale-http://webdezzi.com/motors)
so associative arrays will be something like this (i stand corrected)
headlinex => "this is headline 1"
headlinex => "this is headline 2"
headlinex => "this is headline 3"
headlinex => "this is headline 4"
that way, we can just iterate or loop over the arrays to echo them out and that is where the "while" come in
while ($row = mysql_fetch_assoc($result)){
we are simply saying, while $row is still being equal to mysql_fetch_assoc($result)
then keep running the code within the curly brackets "{ }" (i will explain why we have just one bracket in one second)
so as long as the while gets evaluated to something other than nothing, it will keep running whatever u have in the curly brackets
Why do we have just one curly bracket {
that is because the other bracket is somewhere at the bottom of the script <?php }?>
see how we wrapped it in a php tag, Interesting! thats because we want php to use it, remember!
so you see that everything inside these curly braces including the html codes stand a chance of repeating itself if the database returned more than one result set
so if the news in our database is 2, we will end up with 2 tables
5 news means 5 tables.
here is the DYNAMIC table that is capable of showing up many times
<table width="528" border="0" cellpadding="0" cellspacing="1" bgcolor="#000000">
<tr>
<td bgcolor="#FFFFFF"><strong>Headline:<?php echo $row['headlinex'];?></strong></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">Main Story:<?php echo $row['storyx'];?></td>
</tr>
<tr>
<td bgcolor="#FFFFFF">Posted on <?php echo $row['datex']?> by <?php echo $row['posterx'];?></td>
</tr>
</table>
look inside the table and u find some php codes right inside it, that may want to be confusing but it is not.
PHP allows its programmers to jump in and out of php at any time, as long as you follow the rules.
here is an examples of how to do this (i have html in bold)
<?php if($dog=$animal) {?>
<img src="dog.jpg"> <?php }else{ ?>
<img src="human.jpg"> <?php } ?>
see as i opened and closed the php tags, you can actually continue the rest of your php code later on in your script as long as you respect the open and close tags
in this case, i did not use the ";" and thats because it may not be necessary in a situation like this.
so once this executes, you get to see the dog image if the "if" condition evaluates to true, else, you will see a human image
note also that there are restrictions to this. check your php manual for this. php manual is at php.net, look for the documentation link. (I downloaded the .chm version, that way, i dont need the internet in order to know what a particular function requires.)
back to where we stopped
the php code inside the table above will only do the job of displaying the news associatively
next we will build the admin section where we will be adding news so we can see the result of what we have done so far visually.
Hopefully, that will be tomorrow (and sorry for the previous delays)
PLEASE NOTE, if() while() and switch() are not function, they are called constructs.
and also note that you can define your own functions but its name must not be that of any php functions available
so i can define mine function as
function MyfirstFunction()
{
echo "how are you";
}
so my function will be called like this, notice the case sensitivity
MyfirstFunction();
and the result will be
How are you