Jump to content

Recommended Posts

Hi all, i have a list of article that a pulled from my database, they are listed by name n order of their id. i then wish for the user to be able to click the article name and view the article on its own page. however, the way i add articles to the database is by using a form to submit them. i dont know if i should then create a page for that article automatically using php or if there is a way to display certain information depening on which article is selected for view. please could someone make a suggestion on my best bet. i have placed both scripts below. thanks in advance.

 

add article script:

<?php
    $usr = "user";
    $pwd = "pword";
    $db = "db";
    $host = "localhost";

    # connect to database
    $cid = mysql_connect($host,$usr,$pwd);
    if (!$cid) { echo("ERROR: " . mysql_error() . "\n");    }

?>
   </font>
<P>Add Article </P>
<P>To add an article please fill in the 'Article name' field and then select the category to place it in from the 'Category' drop down, next type the text for the article in the 'Article text' box, finally click the 'Add Article' button. </P>
<font face="Arial, Helvetica, sans-serif">
   <?php
    # this is processed when the form is submitted
    # back on to this page (POST METHOD)
    if ($REQUEST_METHOD=="POST") {

        # double-up apostrophes
        $Article = str_replace("'","''",$Article);
	$Text = str_replace("'","''",$Text);

        # setup SQL statement
        $SQL = " INSERT INTO $Catname ";
        $SQL = $SQL . " (Article, Text) VALUES ";
        $SQL = $SQL . " ('$Article','$Text') ";


        #execute SQL statement
        $result = mysql_db_query($db,"$SQL",$cid);
	$ID=mysql_insert_id();


        # check for error
        if (!$result) { echo("ERROR: " . mysql_error() . "\n$SQL\n");    }

        echo ("<P>New article <b>'$Article'</b> added to <b>'$Catname'</b></P>\n");

    }

?>
</font>
<FORM NAME="fa" ACTION="index.php?page=addArticle" METHOD="POST">
  <TABLE width="392">
<TR>
  <TD width="133"><font face="Arial, Helvetica, sans-serif"><B>Article name :</B> </font></TD><TD width="247"><font face="Arial, Helvetica, sans-serif">
    <INPUT NAME="Article" TYPE="text" id="Article" SIZE=40>
  <label></label>
  </font></TD>
</TR>
<tr>
<td><font face="Arial, Helvetica, sans-serif"><b>Category:</b></font></td>
<td><font face="Arial, Helvetica, sans-serif">
  <select name="Catname" id="Catname">
    <option value="Animals">Animals</option>
    <option value="Books">Books</option>
    <option value="Caravanning and Motorhoming">Caravanning and Motorhoming</option>
    <option value="Cars">Cars</option>
    <option value="Computing and IT">Computing and IT</option>
    <option value="Culture">Culture</option>
    <option value="Film">Film</option>
    <option value="Finance">Finance</option>
    <option value="Food and Drink">Food and Drink</option>
    <option value="Gaming">Gaming</option>
    <option value="Hair">Hair</option>
    <option value="Health and Beauty">Health and Beauty</option>
    <option value="History">History</option>
    <option value="Home and Garden">Home and Garden</option>
    <option value="Music">Music</option>
    <option value="News">News</option>
    <option value="Shopping">Shopping</option>
    <option value="Sport">Sport</option>
    <option value="Television">Television</option>
            </select>
</font></td>
</tr>
<tr>
<td valign="top"><font face="Arial, Helvetica, sans-serif"><b>Article Text:</b></font></td>
<td><label>
  <textarea name="Text" cols="100" rows="50" id="Text"></textarea>
</label></td>
</tr>
<TR>
  <TH COLSPAN=2><font face="Arial, Helvetica, sans-serif">
    <input name="submit" type="submit" value="Add Article" />
  </font></TH>
</TR>
</TABLE>
</FORM>


   <font face="Arial, Helvetica, sans-serif">
   <?    mysql_close($cid); ?>

 

View article list script:

<?php
$con = mysql_connect("localhost","user","pword");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("db", $con);


   $query = "SELECT * FROM `Animals` ORDER BY `ID`";

        $result = mysql_query($query);

$num=mysql_num_rows($result);
$i=0;


while ($i < $num) {
$cat=mysql_result($result,$i,"Article");
$url="index.php?page=";

echo '<tr><td width=5></td><td align=center><a href="index.php?page='.$cat.'">'.$cat.'</a></td></tr>';

++$i;
}
?>

Link to comment
https://forums.phpfreaks.com/topic/88053-solved-solutions-to-a-problem/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.