For those of you who have been watching my posts for the past week, you will see i was working on an article output script that handles multiple pictures from a directory and the article from the database...
it was quite a long process but the script is fairly stable... many elements were used... and i thought it would be a good idea to give you guys the entire script so that you may use it or expand on it.. or even just give me your thoughts on it...
so here it is:
<?php
//Connection information
$dbhost = "localhost";
$dbuser = "xxxxx";
$dbpass = "xxxxx";
$dbname = "xxxxx";
// 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");
// Set up Month Array
$month_text = array (1 => "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
if (!$article) {
// Get latest article
$sql = "Select * FROM loc_news ORDER BY year DESC, month DESC, day DESC, auto_id DESC LIMIT 0, 1";
$result = mysql_query($sql)
or die(mysql_error());
$num_rows = mysql_num_rows($result);
while ($row = mysql_fetch_array($result)) {
$auto_id = $row["auto_id"];
$year = $row["year"];
$month = $row["month"];
$day = $row["day"];
$headline = $row["headline"];
$summary = $row["summary"];
$courtesy_of = $row["courtesy_of"];
$body = $row["body"];
echo "
<font size=5><B>$headline</B></font>
<br>
$month_text[$month] $day, $year
<br>
";
// Dealing with the pictures
function getFiles($dirname, $key) {
$handle=opendir($dirname);
while ($file = readdir($handle))
{
if($file=='.'||$file=='..')
continue;
else
{
if (eregi($key, $file)) { $result_array[]=$file; }
}
}
closedir($handle);
return $result_array;
}
$a = '^' . $auto_id . "_";
$pictures = getFiles("images", $a);
$num_pics = count($pictures);
$paragraphs = explode("<br><br>",$body);
$num_para = count($paragraphs);
if (!$pictures) {
$space = 1;
}
else {
$space = $num_para / $num_pics;
}
$num_pics = $num_pics-1;
$num_para = $num_para-1;
$space = round($space);
$pic_count = 0;
$dev = $space;
for ($para_count = 0; $para_count <= $num_para; $para_count++) {
if ($pic_count <= $num_pics && $para_count == 0) {
echo "
<p>
<img src=images/$pictures[$pic_count] border=1 vspace=10 hspace=10 "; if ($pic_count % 2 == 0) { echo " align=right "; } else { echo " align=left "; } echo " > $paragraphs[$para_count]
</p>
";
$pic_count++;
}
else if ($pic_count <= $num_pics && $para_count == $dev) {
echo "
<p>
<img src=images/$pictures[$pic_count] border=1 vspace=10 hspace=10 "; if ($pic_count % 2 == 0) { echo " align=right "; } else { echo " align=left "; } echo " > $paragraphs[$para_count]
</p>
";
$pic_count++;
$dev = $dev+$space;
}
else {
echo "
<p>
$paragraphs[$para_count]
</p>
";
}
}
}
}
?>
Good Luck!