Hi, I'm pretty new to the php/mysql world and am building an article database for a website with multiple content types. I have an entry screen built that allows the site owner to enter articles, set article types, and set articles as features. Each page in the site calls the database for a feature article for that pages content type, then calls again for the 5 newest articles of that content type excluding any article that is identified as a feature.
I have this mostly working but it is just kind of hacked together. My question is how do I make this more efficient? Code is below
The Submit screen: -
<?php
-
include(login file);
-
$link = mysql_connect($hostname, $user, $password);
-
mysql_set_charset("joshai_article",$link);
-
$db_selected = mysql_select_db('joshai_article', $link);
-
if (!$db_selected) { die ('Database access error : ' . mysql_error());}
-
// Validate this user
-
$test_username = $_POST['test_username'];
-
$query = "SELECT password
-
FROM login
-
WHERE username = '$test_username'";
-
$result = mysql_query($query);
-
if (mysql_num_rows($result) != 1) {
-
echo "Something is wrong";
-
exit;
-
}
-
$password_row = mysql_fetch_array($result);
-
$db_password = $password_row[0];
-
-
if ($_POST['test_password'] == $db_password &&
-
$_POST['test_password'] != "") {
-
if ($_POST['Submit'] == 'Enter') {
-
// Enter new entry
-
$date = date('Ymd'); // Remember, date is an integer type
-
$article = $_POST['article'];
-
$author = $_POST['author'];
-
$byline = $_POST['byline'];
-
$title1 = $_POST['title'];
-
$title = addslashes($title1);
-
$feature = ($_POST['chkfeature'] == 1) ? "1" : "0";
-
switch($_POST['type']){
-
case '1':
-
$type = '1';
-
break;
-
case '2':
-
$type = '2';
-
break;
-
case '3':
-
$type = '3';
-
break;
-
case '4';
-
$type = '4';
-
break;
-
case '5';
-
$type = '5';
-
break;
-
default:
-
$type = '0';
-
}
-
-
$query = "INSERT INTO content (ID, date, title, author, article, byline, approved, feature)
-
VALUES(null, $date, '$title', '$author', '$article', '$byline','1', '$feature')";
-
mysql_query($query) OR die(mysql_error());
-
$article_ID = mysql_insert_id();
-
$query = "INSERT INTO content_type (ID, type_ID, article_ID) VALUES (NULL,$type,$article_ID)";
-
-
$result = mysql_query($query);
-
-
if (mysql_affected_rows() == 1) {
-
header("Location: db_login.php");
-
} else {
-
echo "There was a problem inserting your text.";
-
exit;
-
}
-
} else {
-
// Show the form
-
$php_self = $_SERVER['PHP_SELF'];
-
$test_password = $_POST['test_password'];
-
$form_str = <<< EOFORMSTR
-
-
<HTML>
-
webform goes here</html>
-
?>
The code for displaying content in content div of page -
<? // Open database connection
-
include("login file");
-
mysql_connect($hostname, $user, $password);
-
mysql_select_db("joshai_article");
-
-
// Identify the featured entry for this section
-
$entropia= "SELECT content.ID, content.title, content.author, content.byline, content.article FROM content, content_type, subject
-
WHERE content.ID = content_type.article_ID
-
AND feature=1
-
AND content_type.type_ID = subject.type_ID
-
AND subject.type_ID = 5
-
AND content.approved = '1'";
-
// Print the feature article at top of content div
-
$result1= mysql_query($entropia)
-
or die("dude you screwed up");
-
while (list($link, $title, $author, $byline, $article1)=
-
mysql_fetch_array($result1)){
-
$article=stripslashes($article1);
-
-
echo "<h2>$title </h2>";
-
echo "<h4>$author </h4>";
-
echo "$byline <br>";
-
echo nl2br("$article");
-
-
//echo "<a href=\"view.php?id=". $link."\">View</a>";
-
echo "<br><hr />";
-
}
-
-
// Identify the latest 5 articles for this page type while excluding any featured article
-
$query = "SELECT content.ID, content.title, content.author, content.byline, content.article FROM content, content_type, subject WHERE content.ID = content_type.article_ID
-
AND content_type.type_ID = subject.type_ID
-
AND subject.type_ID = 5
-
AND content.approved = '1' AND content.feature != '1' ORDER BY content.article_ID DESC LIMIT 5
-
-
";
-
$result = mysql_query($query)
-
or die("something is wrong");
-
-
// print the latest 5 articles of this content type to the content div
-
-
while (list($link, $title, $author, $byline, $article1)=
-
mysql_fetch_array($result)){
-
$article=stripslashes($article1);
-
-
echo "<h2>$title </h2>";
-
echo "<h4>$author </h4>";
-
echo "$byline <br>";
-
echo nl2br("$article");
-
-
//echo "<a href=\"view.php?id=". $link."\">View</a>";
-
echo "<br><hr />";
-
}
-
?>
I am trying to set up a code to grab an id from the page url to insert into the subject_type.ID field so I can just use an include to put this script in every page without having to manually go in and set the ID number for every new content category.
Any suggestions would really help this fish out of water.
1 2469 @joshai
Your script is barely acceptable and could use a good organization and clean up. Consider classes or at least a database class.
what exactly do you need to grab an ID? You want a script that grabs a value from the URL parameter and puts it into a common variable? so you can include this in your scripts and access that variable without repeating the script? If this is the case, you need to meeting $_GET[], $_POSTS[] younger sister.
Let me know,
Dna
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Glenn |
last post by:
Hi,
I'm using cygwin and am trying to install DBI-mSQL and am getting errors
on make .. any idea how to fix this (TIA):
cpan> install DBD::mSQL
Running install for module DBD::mSQL
Running...
|
by: Gill Bates |
last post by:
so DOM calls should be calculated from Diff.
Possible?
How? (some URLs please)
I guess this is in most cases not very efficient, but here's why:
I have a data Document at the server.
From...
|
by: Chris Tanger |
last post by:
I am creating a class that has a method "Write" that I wish to make threadsafe. The method must block calling threads until the task performed in write is complete. Only 1 thread at a time can...
|
by: Chapman Flack |
last post by:
Ok, I've registered all the right headers to send, with
header( 'Content-Type: .../...') etc.
The next thing to do is exec a program that will be producing the
actual content. The content MUST...
|
by: MatchSQL |
last post by:
When we want to save the database schema and SQL statement ,
we
use the backup of SQLServer provide, but it always content the running
data, now a new tool named MatchSQL, it can just save...
|
by: yogesh |
last post by:
if i run my make file i am getting all the lines error .... can any
one tell abt this
my make file consist of the detais
# cat Makefile_adm
PRJ=adm.cgi
_INSTALLDIR=../bin
INSTALLUSER=root...
|
by: ABITECH |
last post by:
hi guys
I am working on a project in databases using the wamp software. So how do i connect msql to php. Pliz help mi Because i badly nid the code for my project.
bye
|
by: Atropo |
last post by:
Hi all.
Having several strings how do i combine them to construct a command;
lets say to run the date command.
string str = "14/10/08 19:06:09";
strDD = str.substr(0,2);
strMM =...
|
by: chazzy69 |
last post by:
Ok this may seem a little strange but what i Im trying to add an altered version of information already stored in my database.
Basically i have a colum that has strings of numbers and characters,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| |