Page 1 of 1

Help with PHP news CMS code

Posted: Mon Feb 15, 2010 7:10 am
by azegurb
hi all PHP masters
I have took from internet news publishing script
I read all of code but i have a bit misunderstandings from the code so i dont understand some codings there and i have one question. does anyone help me to improve performance of this code
below i posted code.

Code: Select all

CREATE TABLE `news` (
  id int(11) NOT NULL auto_increment,
  tema varchar(200) NOT NULL default '',
  author varchar(100) NOT NULL default '',
  date date NOT NULL default '0000-00-00',
  text text NOT NULL,
  PRIMARY KEY  (id)
) TYPE=MyISAM;
news.php

Code: Select all

<?
 
 
@$db=mysql_connect('localhost','user DB','password DB');
mysql_select_db('database name');
 
 
 
$per_page=10;
 
 
@$action=$_GET['action'];
@$id=$_GET['id'];
 
 
 
if (!$id && $action){
 
 
if (isset($_GET['page'])) $page=($_GET['page']-1); else $page=0;
$start=abs($page*$per_page);
$q="SELECT count(*) FROM `news`";
$res=mysql_query($q);
$row=mysql_fetch_row($res);
$total_rows=$row[0];
$num_pages=ceil($total_rows/$per_page);
 
 
echo '<h1>news projects</h1>';
$sql="SELECT * FROM `news` ORDER BY `id` DESC LIMIT ".($page*$per_page).",".$per_page;
$result=mysql_query($sql);
$num_results=mysql_num_rows($result);
for ($i=0; $i<$num_results; $i++)
    {
    $row=mysql_fetch_array($result);
 
    $id=$row["id"];
    $author=$row["author"];
    $date=$row["date"];
    $tema=$row["tema"];    
    $text=$row["text"];
 
    echo '<b>'.$tema.'</b><br>
 
    <a href="/news/'.$id.'/">added</a>: <b>'.$author.'</b> 
 
('.$date.')<p> '.$text.' <hr>';
    }
 
   
for($i=1;$i<=$num_pages;$i++) {
  if ($i-1 == $page) {
    echo 
 
"[".(abs($i*$per_page)-$per_page+1)." - ".abs($i*$per_page)."]  ";
  } 
  else {
    echo '[<a 
 
href="news.php?action=all&page='.$i.'">'.(abs($i*$per_page)-$per_page+1)." - ".ab
 
s($i*$per_page)."</a>]  ";
  }
}    
    
}
 
if (!$action && !$id){
 
$sql="SELECT * FROM `news` ORDER BY `id` DESC LIMIT 0,10";
$result=mysql_query($sql);
$num_results=mysql_num_rows($result);
 
for ($i=0; $i<$num_results; $i++)
    {
    $row=mysql_fetch_array($result);
    $id=$row["id"];
    $author=$row["author"];
    $date=$row["date"];
    $tema=$row["tema"]; 
    echo '('.$date.') <a href="news.php?id='.$id.'">'.$tema.'</a><p>';
    }    
}
 
 
if (!$action && $id){
 
$sql="SELECT * FROM `news` WHERE `id`=".$id;
$result=mysql_query($sql);
 
    $row=mysql_fetch_array($result);
    $id=stripslashes($row["id"];
    $author=$row["author"];
    $date=$row["date"];
    $text=$row["text"];
    $tema= $row["tema"]; 
    echo '<h1>'.$tema.'</h1>
 
    added: <b>'.$author.'</b> ('.$date.')<p> '.$text.' <p>
    <a href="news.php?action=all">? return to news</a>';
 
}
 
?>
admin part of the code
news-admin.php

Code: Select all

<?
 
$per_page=10;
?>
<table cellpadding=2 cellspacing=2>
<tr><td>
<?
@$action=$_GET['action'];
 
switch($action):
 
 
case “”: 
$action='edit';
break;
 
 
case “add”:
?>
<FORM ACTION="news-admin.php?action=insert" METHOD="post">
<TABLE WIDTH="600" BORDER=0 CELLPADDING=4 CELLSPACING=0 ALIGN="center" >
 
<TR><TD WIDTH="25%"><b>news theme</b></A></TD>
<TD WIDTH="70%"><INPUT TYPE="text" NAME="tema" SIZE="70" 
MAXLENGTH="80" VALUE=""></TD></TR>
<TR><TD WIDTH="25%"><b>
 
who added</b></A></TD>
<TD WIDTH="70%"><INPUT TYPE="text" NAME="author" SIZE="20"
 MAXLENGTH="80" VALUE=""></TD></TR>
<TR><TD WIDTH="25%"><b>Date</b></A></TD>
 
<TD WIDTH="70%"><INPUT TYPE="text" NAME="date" 
SIZE="20" MAXLENGTH="80" 
VALUE="<?=date("Y-m-d");?>"></TD></TR>
<TR><TD WIDTH="100%" COLSPAN=2><b>
text of news</b></A></TD></TR>
 
<TR><TD WIDTH="100%" COLSPAN=2 ALIGN="center">
<TEXTAREA NAME="text" ROWS="10" COLS=”80”></TEXTAREA></TD></TR>
 
<TR><TD WIDTH="100%" COLSPAN=2 ALIGN="right">
<INPUT TYPE="submit" VALUE="publish news">
</TD></TR></TABLE>
 
</FORM>
<?
break;
 
 
case “insert”:
 
 
$tema=addslashes($tema);
$author=addslashes($author);
$date=addslashes($date);
$text=addslashes($text);
$sql="INSERT INTO `news` 
VALUES ('','".$tema."','".$author."','".$date."','".$text."')";
$result=mysql_query($sql);
if (!$result) {echo "<SCRIPT>alert('error in query 
 
DB!');</SCRIPT>";}
else {echo "<SCRIPT>alert('news added');</SCRIPT>";}
echo "<SCRIPT>self.location.replace('news-admin.php');</SCRIPT>";
break;
 
case “edit”:
 
 
if (isset($_GET['page'])) $page=($_GET['page']-1); else $page=0;
$start=abs($page*$per_page);
 
$q="SELECT count(*) FROM news";
$res=mysql_query($q);
$row=mysql_fetch_row($res);
$total_rows=$row[0];
 
$sql="SELECT * FROM `news` ORDER BY `id` DESC LIMIT ".($page*$per_page).",".$per_page;
$result=mysql_query($sql);
$num_results=mysql_num_rows($result);     
 
$num_pages=ceil($total_rows/$per_page);
 
echo "<div align=right>";
    
for($i=1;$i<=$num_pages;$i++) 
{
  if ($i-1 == $page) 
  {
    echo 
 
"[".(abs($i*$per_page)-$per_page+1)." - ".abs($i*$per_page)."]  ";
  } else 
  {
    echo '[<a 
 
href="news-admin.php?page='.$i.'">'.(abs($i*$per_page)-$per_page+1)." - ".abs($i*
 
$per_page)."</a>]  ";
  }
} echo "</div>";
    
echo "<TABLE WIDTH=100% BORDER=0 CELLSPACING=0 CELLPADDING=5>";
    
for ($i=0; $i<$num_results; $i++)
    {
    $row=mysql_fetch_array($result);
    $id=$row["id"];
    $author=$row["author"];
    $date=$row["date"];
    $text=$row["text"]; 
    $tema=$row["tema"]; 
 
    echo '
 
    <TR><TD COLSPAN=2><b>'.$id.'. '.$tema.'</b>
 
    </TD></TR>
    <TR><TD COLSPAN=2><B>author:</B> '.$author.'  
    [<a href="news-admin.php?action=edit-news&id-news='.$id.'">edit 
 
news</a>]  
    [<a href="news-admin.php?action=delete&id-news='.$id.'">delete news</a>]
    </TD></TR>
 
    <TR><TD COLSPAN=2 CLASS=normal>'.$text.'
    </TD></TR>
    ';
    }
    echo "</TABLE><br><div align=right>";
    
for($i=1;$i<=$num_pages;$i++) 
{
  if ($i-1 == $page) 
  {
    echo 
 
"[".(abs($i*$per_page)-$per_page+1)." - ".abs($i*$per_page)."]  ";
  } else 
  {
    echo '[<a 
 
href="news-admin.php?page='.$i.'">'.(abs($i*$per_page)-$per_page+1)." - ".abs($i*
 
$per_page)."</a>]  ";
  }
} echo "</div><br>";
 
break;
 
 
case “edit-news”:
 
$id_news=$_GET['id-news'];
 
 
$sql="SELECT * FROM `news` WHERE `id`=".$id_news;
$result=mysql_query($sql);
$num_results=mysql_num_rows($result);     
    
for ($i=0; $i<$num_results; $i++)
    {
    $row=mysql_fetch_array($result);
    $id=$row["id"];
    $author=$row["author"];
    $date= $row["date"];
    $text=$row["text"]; 
    $tema=$row["tema"];
 
    echo '
    <FORM ACTION="news.php?action=update" METHOD="post">
 
    <TABLE WIDTH=600 BORDER=0 CELLSPACING=0 CELLPADDING=4>
    <TR><TD width=100>theme:</td><td> <INPUT TYPE=text NAME=tema 
 
VALUE="'.$tema.'" SIZE=80></td></tr>
    <tr><td width=100>date:</td><td><INPUT TYPE=text NAME=date 
 
VALUE="'.$date.'" size=20>
 
    </TD></TR>
    <TR><TD CLASS=normal 
 
width=100><B>author:</B></td><td><INPUT TYPE=text SIZE=20 
 
NAME=author VALUE="'.$author.'">
    </TD></TR>
 
    <TR><TD COLSPAN=2 CLASS=normal><TEXTAREA NAME="text" ROWS="15" 
 
COLS=80>'.$text.'</TEXTAREA>
    </TD></TR>
    <TR ALIGN=center><TD WIDTH="100%" colspan=2 align=right>
    <INPUT TYPE="hidden" NAME="idup" VALUE="'.$id.'">
 
    <INPUT TYPE="submit" VALUE="save changed result">
    </TD></TR></TABLE><BR><BR></FORM>
    ';
    }
break;
 
 
case “update”:
 
$tema=addslashes($tema);
$author=addslashes($author);
$date=addslashes($date);
$text=addslashes($text);
 
 
$sql="UPDATE `news` SET 
 
`tema`='".$tema."',`date`='".$date."',`text`='".$text."',`author`='".$author."' WHERE 
 
`id`='".$idup."'";
$result=mysql_query($sql);
if (!$result) {echo "<SCRIPT>alert('error query DB!');</SCRIPT>";}
else {echo "<SCRIPT>alert('news changed');</SCRIPT>";}
echo "<SCRIPT>self.location.replace('news-admin.php');</SCRIPT>";
break;
 
 
case “delete”:
 
$tema=addslashes($tema);
$author=addslashes($author);
$date=addslashes($date);
$text=addslashes($text);
 
 
$sql="DELETE FROM `news` WHERE `id`='".$iddel."'";
$result=mysql_query($sql);
if (!$result) {echo "<SCRIPT>alert('alert in query DB!');</SCRIPT>";}
else {echo "<SCRIPT>alert('news deleted');</SCRIPT>";}
echo "<SCRIPT>self.location.replace('news-admin.php');</SCRIPT>";
break;
?>
 
</DIV></td></tr></table>
<?
endswitch;
?>
here i dont understand what does mean

Code: Select all

@$action=$_GET['action'];

Code: Select all

@$id=$_GET['id'];
and

Code: Select all

if (isset($_GET['page'])) $page=($_GET['page']-1); else $page=0;
when is says

Code: Select all

if (isset($_GET['page']))
what does it mean and why not

Code: Select all

$page=($_GET['page'])
and what does it mean

Code: Select all

if (!$action && !$id){
and finally the last question is

Code: Select all

if (!$action && $id){
what does it mean
and I would like to add one field for picture uploads
thanks for attention
i will wait your responses

Re: Help with PHP news CMS code

Posted: Mon Feb 15, 2010 11:24 am
by AbraCadaver
here i dont understand what does mean

Code: Select all

@$action=$_GET['action'];

Code: Select all

@$id=$_GET['id'];
The @ suppresses the undefined variable notice if the $_GET['action'] is not set.

and

Code: Select all

if (isset($_GET['page'])) $page=($_GET['page']-1); else $page=0;
when is says

Code: Select all

if (isset($_GET['page']))
what does it mean and why not

Code: Select all

$page=($_GET['page'])
This is the more proper way to do it instead of the first way with @. The isset() is checking to see if $_GET['page'] is set and if so it is subtracting 1. If not set then it sets a value of 0.

and what does it mean

Code: Select all

if (!$action && !$id){
The ! means not, so if not $action and not $id, which would be false, 0 an empty string or if they are not set.

and finally the last question is

Code: Select all

if (!$action && $id){
Same as the last one except if not $action, but $id is is not false (not 0, false, an empty string or not set).

Posted: Wed Feb 17, 2010 12:39 am
by azegurb
Thank you very much i have one problem too. if possible please see it

Re: Help with PHP news CMS code

Posted: Wed Feb 17, 2010 12:40 am
by azegurb
thank you very much