Jump to content


Photo

checkboxes and DB [solved]


  • Please log in to reply
5 replies to this topic

#1 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 July 2006 - 06:46 PM

Hey guys,

I have checkboxes that are generated with elements from the db and I'd like it when the user clicks on submit it stores whatever he checked into a session so it can be used on another page.

here's what I got in my form
<?php

$pf_time = strtotime("-21 days");
$pf_date = date("Y-m-d", $pf_time);

$result=mysql_query("SELECT * FROM news WHERE date >= " . $pf_date . " ORDER by id") or die(query_error());
$row=mysql_fetch_array($result);
$num_rows = mysql_num_rows($result);

$result2=mysql_query("SELECT * FROM events") or die(query_error());
$row2=mysql_fetch_array($result2);
$num_rows2 = mysql_num_rows($result2);

?>

<div id="article">
<form action="admin.php?a=newnewsletter" method="post" enctype="multipart/form-data" name="form" id="form">
<input type="hidden" name="process" value="yes" />
<input type="hidden" name="size_limit" value="500" />
    <input name="size_limit" type="hidden" id="size_limit" value="500" />
    <table width="603" border="0">
      <tr> 
        <td colspan="2">&nbsp;</td>
      </tr>
      <tr> 
        <td width="151">Select News :</td>
        <td width="442"><? if ($num_rows < 1) {
		echo 'There are no new News';
		} else {
		for ($i=0;$i<$num_rows;$i++) {
		$ID = mysql_result($result,$i,"headline");
		$NA= mysql_result($result,$i,"headline");
		echo '<input type ="checkbox" name="NA[]" value="'.$ID.'">
		'.$ID.'';
		}
		}?></td>
      </tr>
      <tr> 
        <td>Select Events:</td>
        <td><? if ($num_rows < 1) {
		echo 'There are no new News';
		} else {
		for ($i=0;$i<$num_rows2;$i++) {
		$ID2 = mysql_result($result2,$i,"name");
		$DA= mysql_result($result2,$i,"name");
		echo '<input type ="checkbox" name="DA[]" value="'.$ID2.'">
		'.$ID2.'';
		}
		}?></td>
      </tr>
      
      <tr>
        <td>Type:</td>
        <td><select name="type" id="type">
          <option>Choose one</option>
          <option value="house">House</option>
          <option value="hip hop">Hip Hop</option>
          <option value="both">Both</option>
        </select>
        </td>
      </tr>
      
    </table>
<input name="send" type="submit" id="send" value="Submit"/>
</form>
</div>

I want all that info to be stored in a session,

here's what I got so far in admin.php

$process = $_POST['process'];
      if ($process == "yes") {
          $qtmp = array();
          $wtmp = array();
          foreach($_POST['NA'] as $k => $v) {
          $qtmp[] = $v;
          }
           foreach($_POST['DA'] as $c => $b) {
           $wtmp[] = $b;
}
//...

that grabs everything that's been checked but how can I reuse that on another page, let's say someone checks news 1, news 2, news 6 I'd like to take that info, query the news table and get all the info for each of those headlines...

how can I do this?

#2 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 July 2006 - 11:03 PM

no one?

ok I'll simplify things, I need to query a table depending on what a user checks

so let's say I have this in my table

+----------------------------------------+
| id | headline | content | date                |
+----------------------------------------+
| 1 | test1 | blablab | 2006-07-09            |
+----------------------------------------+
| 2 | test2 | blablab2 | 2006-07-09          |
+----------------------------------------+
| 3 | test3 | blablab3 | 2006-07-09          |
+----------------------------------------+

and in the form the user checks test1 and test3

when the form is processed everything that got checked goes in an array like so

$qtmp = array();
foreach($_POST['NA'] as $k => $v) {
     $qtmp[] = $v;
}

now I want to query the databse so that it displays the data of everything in test1 and test3,  Itried doing this but it just gives me an error

$result=mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('. implode(',', $qtmp). '") or die(query_error());
while ($row=mysql_fetch_array($result)) {
echo $row['content'];
}

what am I doing wrong??


#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 09 July 2006 - 11:10 PM

Try
<?php
$DAList = implode ("','", $_POST['DA']);
$result=mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('$DAList')") or die(query_error());
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#4 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 09 July 2006 - 11:34 PM

thanks Barand that worked perfectly.

Do you know how I can retrieve the info for each row seperatly? right now if I echo $row['content'] I get blablabblablab3  (test1 and test3 combined together) but I want to be able to echo the headline and content of each news seperatly


#5 digitalgod

digitalgod
  • Members
  • PipPipPip
  • Advanced Member
  • 374 posts

Posted 10 July 2006 - 05:02 PM

so is there a way to actually get each content row seperatly and not all joined together because when I do
<?
while ($row=mysql_fetch_array($result)) {
echo $row['content'];
}
?>

It outputs everything together...

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 10 July 2006 - 06:57 PM

one way
<?php
while ($row=mysql_fetch_array($result)) {
    echo "<p>$row['content']</p>" ;
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users