Jump to content

Echo all data from a column


bullbreed

Recommended Posts

<?php
$sql = "SELECT DISTINCT(article_section) FROM tablename";
$query = mysql_query($sql);
if(mysql_num_rows($query) > 0){?>
<select name="categories">
<?php
while($r = mysql_fetch_array($query)){
	echo '<option value=" ' .$r['article_section'] . '">' . $r['article_section'] . '</option>';
}
?>
</select>
<?php } ?>

Wow. Thanks for that it worked a dream.  :D I really appreciate your help. Thank you.

 

Also when the user creates a news article they have an option to either create the article category or select it from the menu that was just sorted, (Thanks again)

 

I had a go at the code but I cant get the article section to go in the database

 

What if I made 2 form elements

exist_cat - This would be a drop down menu form field with a value of 'None'

new_cat - This would be an input text field which would have no value

 

Then I could say

 

If exist_cat doesn't = 'None'

Insert exist_cat in to the database

If exits_cat does = 'None'

Insert into new_cat in to the database

 

Then check the rest of the form and insert that in to the database

 

I had a go at writing the script. Could someone point out if it will work and what's wrong with the code

 

Heres the php

<?php
$submit = $_POST['submit'];

//Form data
$existcat = mysql_real_escape_string($_POST['exist_cat']);
$newcat = mysql_real_escape_string($_POST['new_cat']);
$articletitle = mysql_real_escape_string($_POST['article_title']);
$articleptitle = mysql_real_escape_string($_POST['article_ptitle']);
$articlepdescription = mysql_real_escape_string($_POST['article_pdescription']);
$articlepkeywords = mysql_real_escape_string($_POST['article_pkeywords']);
$articlecontent = mysql_real_escape_string($_POST['article_content']);
$date = date('d/m/Y \a\t g:i.s a');

if ($submit){

//Open the database
    mysql_connect("localhost","root","");
    mysql_select_db("********"); //Select the database

if ($existcat !== 'None'){
	$queryreg = mysql_query("INSERT INTO articles (`article_cat` VALUES '$existcat')");
}else{
	if ($existcat == 'None'){
	$queryreg = mysql_query("INSERT INTO articles (`article_cat` VALUES '$newcat')");
	}
}

	//Check for existing fields
if ($articletitle && $articleptitle && $articlepdescription && articlepkeywords && $articlecontent && $date){


$pagecheck = mysql_query("SELECT `article_title` FROM articles WHERE `article_title` = '$articletitle'");echo mysql_error();
    $count = mysql_num_rows($pagecheck);echo mysql_error();
    
    if ($count != 0){
        echo("<font size=\"2\" color=\"#ff0000\">Article already exists. Please edit the existing article!</font>");echo mysql_error();
    }else{

//Enter into Database
	$queryreg = mysql_query("INSERT INTO articles (`article_title`, `article_ptitle`, `article_pdescription`, `article_pkeywords`, `article_content`, `date`) VALUES ('$articletitle', '$articleptitle', '$articlepdescription', '$articlepkeywords', '$articlecontent', '$date')");
	echo("<font size=\"2\" color=\"#00cc00\">Your article has been created! </font>");
}
}else{
	echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>");
	 }
}	//end Check for existing fields
//end if submit
$sql = "SELECT * FROM `articles` ORDER BY `id` ASC";
$query = mysql_query($sql);

?>

 

and the form

<form action="addarticle.php" method="post" enctype="multipart/form-data" name="addarticle" target="_self" id="addarticle">
        <!-- Form Starts -->
        <div class="titlearea">Add News Article</div>
        <div class="dataarea">
          <div class="infowrap">Article title</div>
          <div class="infowrap">Create Article Section</div>
          <div class="infowrap">Select Article Section</div>
          <div class="infowrap">
            <input name="article_title" type="text" id="article_title" size="25" maxlength="20" />
          </div>
          <div class="infowrap">
            <input name="new_cat" type="text" id="new_cat" size="25" maxlength="100" />
          </div>
          <div class="infowrap">
            <label></label>
            <?php
//Open the database
    mysql_connect("localhost","root","");
    mysql_select_db("********"); //Select the database
$sql = "SELECT DISTINCT(article_section) FROM articles";
$query = mysql_query($sql)or die(mysql_error());
if(mysql_num_rows($query) > 0){
?>
            <label>
              <select name="exist_cat" id="exist_cat">
              <option>None</option>
<?php
while($r = mysql_fetch_array($query)){
	echo '<option value=" ' .$r['article_section'] . '">' . $r['article_section'] . '</option>';
}
?>
</select>
<?php } ?>
              </label>
          </div>
        </div>
        <div class="titlearea">Meta Data</div>
        <div class="dataarea">
          <div class="metawrap">Title - Please use approx 65 characters</div>
          <div class="metawrap">
            <input name="article_ptitle" type="text" id="article_ptitle" size="80" maxlength="20" />
          </div>
          <div class="metawrap">Description - Please use approx 155 characters inc spaces</div>
          <div class="metawrap">
            <input name="article_pdescription" type="text" id="article_pdescription" size="80" maxlength="200" />
          </div>
          <div class="metawrap">Keywords - Please use approx 5 keyword per page</div>
          <div class="metawrap">
            <input name="article_pkeywords" type="text" id="article_pkeywords" size="80" maxlength="200" />
          </div>
        </div>
        <div class="titlearea">Article Content</div>
        <div class="dataarea">
          <textarea name="article_content" cols="80" rows="20" id="article_content"></textarea>
        </div>
        <div class="dataarea">
          <input type="submit" value="Save" name="submit" />
        </div>
      </form>

 

Thanks

I think this bit isn't correct

if ($existcat !== 'None'){
	$queryreg = mysql_query("INSERT INTO articles (`article_cat` VALUES '$existcat')");
}else{
	if ($existcat == 'None'){
	$queryreg = mysql_query("INSERT INTO articles (`article_cat` VALUES '$newcat')");
	}
}

 

looks like you just have your syntax incorrect....try this:

if ($existcat !== 'None'){
	$queryreg = mysql_query("INSERT INTO articles (article_cat) VALUES ('$existcat')");
}else{
	if ($existcat == 'None'){
	$queryreg = mysql_query("INSERT INTO articles (article_cat) VALUES ('$newcat')");
	}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.