FOR COMPLETE PERSPECTIVE ON THE FOLLOWING CODE CHECK THE THREAD: http://www.phpbuilder.com/board/showthread.php?postid=10452986#post10452986
ok right.. so i tried to optimise my code.. and i think i screwed everything up!!!!
i don't know.. the function don't work.. perhaps you can tell me what im doing wrong.. this is the first time im writing a function..
here's the code.. thanks for the help
<?php
//Database Information
$dbhost = "localhost";
$dbuser = "administrator";
$dbpass = "admin";
$dbname1 = "f1bahrain";
$dbname2 = "f1b";
// Connect to MySql
$connect = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Unable to connect to MySql:<br>" .mysql_errno(). " : " .mysql_error());
// Connect to Database F1bahrain
$db = mysql_select_db($dbname1, $connect)
or die("Unable to connect to Database:<br>" .mysql_errno(). " : " .mysql_error());
// Get all articles into an array for easier article selection and counting
$arraySQL = "Select auto_id FROM loc_news";
$arrayResult = mysql_query($arraySQL)
or die("Unable to complete query:<br>" .mysql_errno(). " : " .mysql_error());
// Count the number of articles in the table and array
$noOfArticles = mysql_num_rows($arrayResult);
// Set article ID numbers into an array, starting from one
$arrayCounter = 0;
while ($arrayRow = mysql_fetch_array($arrayResult)) {
$articleID = $arrayRow["auto_id"];
$arrayCounter++;
$article[$arrayCounter] = $articleID;
}
// Print number of articles available based according to the article array
echo "<b><font color=#006699>There are currently $noOfArticles articles in the <i>Local News</i> table</font></b><br>";
// Create function to handle the retrieval of the article to be displayed
function callArticle ($articleChoice) {
// Run query to retrieve article
$sql = "SELECT * FROM loc_news WHERE auto_id = '$articleChoice'";
$result = mysql_query($sql)
or die ("Unable to complete query:<br>" .mysql_errno(). " : " .mysql_error());
// Check the existence of the article
if (!$exist = mysql_num_rows($result)) {
// Run query to retrieve first article
$sql = "SELECT * FROM loc_news WHERE auto_id = '1'";
$result = mysql_query($sql)
or die ("Unable to complete query:<br>" .mysql_errno(). " : " .mysql_error());
$articleDetails = mysql_fetch_array($result);
return $articleDetails;
}
else {
$articleDetails = mysql_fetch_array($result);
return $aticleDetails;
}
}
/* Set variables for array Index and article Id.
This is to make sure I have a value for each,
knowing that I will always have one or the other.*/
if (!$articleIndex) {
$articleIndex = array_search($articleChoice, $article);
}
else {
$articleChoice = $article[$articleIndex];
}
// Decide what action was taken and how to respond
if ($goto) {
// Run the function and break up the array
callArticle($articleChoice);
// Find out if the article existed and print out what action was taken
switch ($exist) {
case '0':
echo "<b>-Sorry, article $articleChoice does not exist. Article 1 is displayed instead:</b>";
break;
default:
echo "<b>-Article $articleChoice was chosen and is displayed [<i>Array Index: $indexArticle</i>]:</b>";
break;
}
}
else {
echo "<b>-No article is chosen, the first article in the table is displayed:</b>";
// Run the function and break up the array
callArticle($articleChoice);
}
?>
there are a couple of else if missing from the last if else structure but right now this is just to get the thing working..
thanks again.