digitalgod Posted July 9, 2006 Share Posted July 9, 2006 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[code]<?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"> </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>[/code]I want all that info to be stored in a session,here's what I got so far in admin.php[code]$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;}//...[/code]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? Quote Link to comment https://forums.phpfreaks.com/topic/14108-checkboxes-and-db-solved/ Share on other sites More sharing options...
digitalgod Posted July 9, 2006 Author Share Posted July 9, 2006 no one?ok I'll simplify things, I need to query a table depending on what a user checksso 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 test3when the form is processed everything that got checked goes in an array like so[code]$qtmp = array();foreach($_POST['NA'] as $k => $v) { $qtmp[] = $v;}[/code]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[code]$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'];}[/code]what am I doing wrong?? Quote Link to comment https://forums.phpfreaks.com/topic/14108-checkboxes-and-db-solved/#findComment-55313 Share on other sites More sharing options...
Barand Posted July 9, 2006 Share Posted July 9, 2006 Try[code]<?php$DAList = implode ("','", $_POST['DA']);$result=mysql_query("SELECT * FROM ".$prefix."news WHERE headline IN('$DAList')") or die(query_error());?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14108-checkboxes-and-db-solved/#findComment-55316 Share on other sites More sharing options...
digitalgod Posted July 9, 2006 Author Share Posted July 9, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14108-checkboxes-and-db-solved/#findComment-55320 Share on other sites More sharing options...
digitalgod Posted July 10, 2006 Author Share Posted July 10, 2006 so is there a way to actually get each content row seperatly and not all joined together because when I do[code]<?while ($row=mysql_fetch_array($result)) {echo $row['content'];}?>[/code]It outputs everything together... Quote Link to comment https://forums.phpfreaks.com/topic/14108-checkboxes-and-db-solved/#findComment-55648 Share on other sites More sharing options...
Barand Posted July 10, 2006 Share Posted July 10, 2006 one way[code]<?phpwhile ($row=mysql_fetch_array($result)) { echo "<p>$row['content']</p>" ;}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/14108-checkboxes-and-db-solved/#findComment-55716 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.