HELLO!!!! and how are you?
ok.. so to understand my code here is what you gotta know.. i learned php4 using a book from wrox. i never did finish the book.. but i got real close to the end.. to be completely honest.. i really learned php by trial and error.. just typing some code!...
well anyway.. the problem with this is that im sure there are many faster, shorter, and more effiecient ways to do what my scripts do... and i wanna get feed back on that.. soooo.. i pasted all my code from the latest script i wrote right here.. below..
the script is for a web form that allows a site administrator to edit the rows in a news table. i actually wrote this script to facilitate the migration of the table to another database.. i wanted to move the table row by row so i can proofread each article, edit it, add stuff to it.. ect... i got pretty much done with the code when i realized that i can just replace the migrate part of the script with edit code and use the page in the future... (for clarification purposes i removed the migrate portion and replaced it with edit code. (it still under the same part of the if_else statement)...
so without further ado.. here is the code.. and now YOU tell ME how can i write it better...!
thanks
<?php
//Database Information
$dbhost = "localhost";
$dbuser = "******";
$dbpass = "******";
$dbname = "*****";
// Connect to MySql
$connect = mysql_connect($dbhost, $dbuser, $dbpass)
or die("Unable to connect to MySql");
// Connect to Database
$db = mysql_select_db($dbname, $connect)
or die("Unable to connect to Database");
// 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 perform SQL Query");
$noOfArticles = mysql_num_rows($arrayResult); // To count the number of articles in the table (and array)
$arrayCounter = 0;
while ($arrayRow = mysql_fetch_array($arrayResult)) {
$articleID = $arrayRow["auto_id"];
$arrayCounter++;
$article[$arrayCounter] = $articleID;
}
//Give information about the table
echo "
<b>
There are currently $noOfArticles articles in the <i>Local News</i> table
</b>
<br>
";
//If Else statements to handle article display
if ($goto) { //If a specific article is chosen, the article is checked for existence and then shown
$indexArticle = array_search($articleChoice, $article);
$gotoSQL = "SELECT * FROM loc_news WHERE auto_id = '$articleChoice'";
$gotoResult = mysql_query($gotoSQL)
or die ("Unable to retrieve Article information");
//Make sure the article called exists and act accordingly
$exist = mysql_num_rows($gotoResult);
if ($exist != 1) {
echo "<b>-Sorry, article $articleChoice does not exist. Article 1 is displayed instead:</b>";
$gotoSQL = "SELECT * FROM loc_news WHERE auto_id = '1'";
$gotoResult = mysql_query($gotoSQL)
or die ("Unable to retrieve Article information");
}
else {
echo "<b>-Article $articleChoice was chosen and is displayed [<i>Array Index: $indexArticle</i>]:</b>";
}
while ($gotoRow = mysql_fetch_array($gotoResult)) {
$auto_id = $gotoRow["auto_id"];
$year = $gotoRow["year"];
$month = $gotoRow["month"];
$day = $gotoRow["day"];
$headline = $gotoRow["headline"];
$summary = $gotoRow["summary"];
$courtesy_of = $gotoRow["courtesy_of"];
$body = $gotoRow["body"];
}
}
else if ($prev) {
if ($current == 1) {
echo "<b>-Sorry, there are no previous articles. Article 1 is displayed:</b>";
$articleChoice = 1;
$prevSQL = "SELECT * FROM loc_news WHERE auto_id = '$articleChoice'";
$prevResult = mysql_query($prevSQL)
or die ("Unable to retrieve Article information");
while ($prevRow = mysql_fetch_array($prevResult)) {
$auto_id = $prevRow["auto_id"];
$year = $prevRow["year"];
$month = $prevRow["month"];
$day = $prevRow["day"];
$headline = $prevRow["headline"];
$summary = $prevRow["summary"];
$courtesy_of = $prevRow["courtesy_of"];
$body = $prevRow["body"];
}
}
else {
$indexArticle = $current-1;
$articleChoice = $article[$indexArticle];
echo "<b>-Article $articleChoice was chosen and is displayed [<i>Array Index: $indexArticle</i>]:</b>";
$prevSQL = "SELECT * FROM loc_news WHERE auto_id = '$articleChoice'";
$prevResult = mysql_query($prevSQL)
or die ("Unable to retrieve Article information");
while ($prevRow = mysql_fetch_array($prevResult)) {
$auto_id = $prevRow["auto_id"];
$year = $prevRow["year"];
$month = $prevRow["month"];
$day = $prevRow["day"];
$headline = $prevRow["headline"];
$summary = $prevRow["summary"];
$courtesy_of = $prevRow["courtesy_of"];
$body = $prevRow["body"];
}
}
}
else if ($next) {
if ($current == $noOfArticles) {
echo "<b>-Sorry, there are no more articles. Article $article[$noOfArticles] is displayed:</b>";
$articleChoice = $article[$noOfArticles];
$nextSQL = "SELECT * FROM loc_news WHERE auto_id = '$articleChoice'";
$nextResult = mysql_query($prevSQL)
or die ("Unable to retrieve Article information");
while ($nextRow = mysql_fetch_array($nextResult)) {
$auto_id = $nextRow["auto_id"];
$year = $nextRow["year"];
$month = $nextRow["month"];
$day = $nextRow["day"];
$headline = $nextRow["headline"];
$summary = $nextRow["summary"];
$courtesy_of = $nextRow["courtesy_of"];
$body = $nextRow["body"];
}
}
else {
$indexArticle = $current+1;
$articleChoice = $article[$indexArticle];
echo "<b>-Article $articleChoice was chosen and is displayed [<i>Array Index: $indexArticle</i>]:</b>";
$nextSQL = "SELECT * FROM loc_news WHERE auto_id = '$articleChoice'";
$nextResult = mysql_query($nextSQL)
or die ("Unable to retrieve Article information");
while ($nextRow = mysql_fetch_array($nextResult)) {
$auto_id = $nextRow["auto_id"];
$year = $nextRow["year"];
$month = $nextRow["month"];
$day = $nextRow["day"];
$headline = $nextRow["headline"];
$summary = $nextRow["summary"];
$courtesy_of = $nextRow["courtesy_of"];
$body = $nextRow["body"];
}
}
}
else if ($edit) {
// Take submitted information and edit the respected row in the table with the new information
$whereClause = $article[$indexArticle];
$tableSQL = "UPDATE loc_news SET year = '$year', month = '$month', day = '$day', courtesy_of = '$courtesy_of', subject = '$subject', headline = '$headline', summary = '$summary', body = '$body' WHERE auto_id = '$whereClause'";
$tableResult = mysql_query($tableSQL)
or die("Unable to complete table update");
// Get next article
$indexArticle = $indexArticle+1;
$articleChoice = $article[$indexArticle];
// Check to see if there are any more articles and act accordingly
if ($indexArticle <= $noOfArticles) {
echo "<b>-Article $articleChoice was successfully edited. The next article is displayed:</b>";
}
else {
echo "<b>-Article $articleChoice was successfully edited. There are no more articles in the table. The first article is displayed:</b>";
$articleChoice = 1;
}
$editSQL = "SELECT * FROM loc_news WHERE auto_id = '$articleChoice'";
$editResult = mysql_query($editSQL)
or die ("Unable to retrieve Article information");
while ($editRow = mysql_fetch_array($editResult)) {
$auto_id = $editRow["auto_id"];
$year = $editRow["year"];
$month = $editRow["month"];
$day = $editRow["day"];
$headline = $editRow["headline"];
$summary = $editRow["summary"];
$courtesy_of = $editRow["courtesy_of"];
$body = $editRow["body"];
}
}
else {
$indexArticle = 1;
echo "<b>-No article is chosen, the first article in the table is displayed:</b>";
$noneSQL = "SELECT * FROM loc_news WHERE auto_id = '$indexArticle'";
$noneResult = mysql_query($noneSQL)
or die ("Unable to retrieve Article information");
while ($noneRow = mysql_fetch_array($noneResult)) {
$auto_id = $noneRow["auto_id"];
$year = $noneRow["year"];
$month = $noneRow["month"];
$day = $noneRow["day"];
$headline = $noneRow["headline"];
$summary = $noneRow["summary"];
$courtesy_of = $noneRow["courtesy_of"];
$body = $noneRow["body"];
}
}
?>