Hello all,
Newbie and running out of steam here.
I have a form passing the checkbox value to a db.

The db is essentially 2 fields news and id (auto-increment).
I'm pretty baffled as how to extract the data.
Ideally I would like to give each form submission a unique ID -
That's probably for another post.

<form method="post" action="processcheck.php">
    
    <p><input type="checkbox" name="news[]" value="newspaper1"> Guardian<br />
    <input type="checkbox" name="news[]" value="independent"> Independent<br />
    <input type="checkbox" name="news[]" value="observer"> Observer<br />
    <input type="checkbox" name="news[]" value="scotsman"> Scotsman<br />
    <input type="checkbox" name="news[]" value="herald"> Herald</p>
    
<div class="row">
 <span class="formlabel"></span>
  <span class="forminput"><input type="submit" value="Submit" class="submit" /> <input type="reset" class="submit" /></span>
</div>
</form>

processcheck.php

<?php
 
  $dbh = mysql_connect('localhost', 'xxxxxx', 'xxxxxx') or die(mysql_error());
mysql_select_db('xxxxxx') or die(mysql_error());
?>
<?php

$news = implode(",",$_POST['news']); 

 for($i = 0; $i < sizeof($news); $i++) {
   $query = "INSERT INTO cBox (news) VALUES ('".$news."')";
      $result = mysql_query($query) or die(mysql_error());
}
mysql_close($dbh);
		
// Echo to the browser.
  
 if(count($news) == 0){echo "<p>No Checkbox selected !</p>";}
   if (count($news) > 0) {
      for ($i=0;$i<count($news);$i++) {
        echo "<li>$news[$i] \n";
  }
}		
			
?>

Now I can see the db being populated with "array".

I created a page for testing and to extract the db data - This is where I'm baffled.

getnews.php

<?php

//Connecting to the db here

?>
<?php

 $result = mysql_query("SELECT *  news FROM cBox");
 echo "<hr />".$result."<hr />";
 while($info = mysql_fetch_array($news, MYSQL_ASSOC)) {
//echo "<hr />".$info."<hr />";
//echo "<hr />".$news."<hr />";
//exit();
 $news = explode(",", $info['news']);
   foreach ($news as $value){
    echo $value;
    echo "<br/>";
    //echo "<li>$value\n";
  }
}

?>

Okay scratch that.
My bad.
Did not check phpMyAdmin.
I am getting the string values in the db and not "array" as previously posted -

<?php

$news = implode(",", $_POST['news']); 
   for($i = 0; $i < sizeof($news); $i++) {
        $query = "INSERT INTO cBox (news) VALUES ('".$news."')";
        $result = mysql_query($query) or die(mysql_error());
  }
  mysql_close($dbh);
		
// Echo to the browser.
   $news = $_POST['news'];
      if(count($news) == 0){echo "<p>No Checkbox selected !</p>";}
	if (count($news) > 0) {
         for ($i=0;$i<count($news);$i++) {
         echo "<li>$news[$i] \n";
  }
}		
			
?>

Now how do I pull these values with PHP to display in a browser ?

Use a select query, explode the values and then loop through them.

$sql   = "SELECT `news` FROM `cBox`"; //you probably need to add a where clause to restrict that amount of data
$query = mysql_query( $sql );
$total   = mysql_num_rows( $query );
if ( $total > 0 ) {
  while ( list( $news ) = mysql_fetch_row( $query ) ) {
    $news = explode( ',',$news );
    //now do whatever you want with the news array.
  }
}

kkeith29,
Thank you.
That works.:icon_cheesygrin:
I added an "if else" to catch if no array data is being passed.
Added this to the code you provided -

if (count($news) > 0) {
 for ($i=0;$i<count($news);$i++) {
   echo "<li>$news[$i] \n";
  }
}

"Just one more thing" - The Mighty Columbo :)

I would like to display the data in the browser similar to the SQL Result (id news table) - [See attached].
I'm OK with the HTML formatting just wondering how to go about that. Grabbing the id etc.

@kkeith29 - Appreciated.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.