garethhall Posted June 10, 2009 Share Posted June 10, 2009 Can you please point me in the right direction? As you can see from the code below I am creating a checkbox with a name of "photoNo" and a value of "photoID". And I call the values fine on the second page but..............................................The thing is I need to add mores values per each checkbox. How do do this. I need to have the checkbox to hold the following values photoID, clientId and albumId. How can I add these values and call them in the next page. Ohh and why is my code not showing in colour on this website? <? //==============List All Albums that the Photo is in================================ function listPhotos($photoName, $admin){ $SQL_selectPhotoName = "SELECT photos.photoID, photos.photoName, photos.originalName, albums.albumName FROM photos INNER JOIN albums ON photos.albumId = albums.albumID WHERE photos.photoName = '$photoName'"; $rs_selectPhotoName = mysql_query($SQL_selectPhotoName, $admin); $no_selectPhotoName = mysql_num_rows($rs_selectPhotoName);//check the number of times the photo is listed in the DB $ar_albums = array(); while($row_selectPhotoName = mysql_fetch_assoc($rs_selectPhotoName)){ $ar_albums[] = $row_selectPhotoName; } if($no_selectPhotoName > 1){ //if photo is in DB more that once then list album names foreach($ar_albums as $name){ echo "A: ".$name['albumName']." <input type='checkbox' name='photoNo[]' value='".$name['photoID']."' id='checkbox' /><br>"; } }else{ // if photo is listed only once the list album name foreach($ar_albums as $name){ echo "A: ".$name['albumName']." <input type='checkbox' name='photoNo[]' value='".$name['photoID']."' id='checkbox' /><br>"; } } } ?> This is the second page where I want to call the selected value <? foreach($_POST['photoNo'] as $key => $value){ $SQL_selectPhoto = "SELECT * FROM photos WHERE photoID = '$value'"; $rs_selectPhoto = mysql_query($SQL_selectPhoto, $admin); $row_selectPhoto = mysql_fetch_assoc($rs_selectPhoto); echo "F: ".$row_selectPhoto['originalName']."<br />"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/ Share on other sites More sharing options...
MadTechie Posted June 10, 2009 Share Posted June 10, 2009 Just add some hidden inputs <input type="hidden" name="clientId" value="2"> <input type="hidden" name="albumId" value="5"> Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/#findComment-852818 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2009 Share Posted June 10, 2009 In general you would use one value that also identifies the other values, but if you want to actually pass multiple values through the form data for each checkbox, you would place all the values into the value="..." attribute and separate them with a character that will never appear in any of the data. A pipe character | is often used. You would then explode using the | once the data reaches the server. Make sure you are not relying on any of the values for security purposes, such as limiting which user id can edit a value, because a hacker will quickly discover that he can set any of the values to anything he wants and modify other user's records. Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/#findComment-852820 Share on other sites More sharing options...
ted_chou12 Posted June 10, 2009 Share Posted June 10, 2009 Ohh and why is my code not showing in colour on this website? either use or put <?[b]php[/b]. To store multiple values just used: <input type="checkbox" name="values[]"> the values would be in an array, so you should use $values = $_POST['values']; to get then in an array. Ted Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/#findComment-852841 Share on other sites More sharing options...
garethhall Posted June 10, 2009 Author Share Posted June 10, 2009 Great you guys have given me ideas and I have been working on implementing them but I hit a small problem with a switch function now. My $_POST['photoNo'] will be something like "50|2|8" 50 being the photoID, 2 being the albumId and 8 being the clientID....... Now the way I understand this is that my last foreach loop will run 3 time meaning I should get the result from my switch of "one two three" but I am getting a result of "one two three one two three one two three" If I remove the condition $i = 0 in case 3 then my result change to "one two three two three three" Why is this so? Ok so my first page code now looks like this <?php function listPhotos($photoName, $admin){ $SQL_selectPhotoName = "SELECT photos.photoID, photos.photoName, photos.originalName, photos.albumId, photos.clientId, albums.albumName FROM photos INNER JOIN albums ON photos.albumId = albums.albumID WHERE photos.photoName = '$photoName'"; $rs_selectPhotoName = mysql_query($SQL_selectPhotoName, $admin); $no_selectPhotoName = mysql_num_rows($rs_selectPhotoName);//check the number of times the photo is listed in the DB $ar_albums = array(); while($row_selectPhotoName = mysql_fetch_assoc($rs_selectPhotoName)){ $ar_albums[] = $row_selectPhotoName; } if($no_selectPhotoName > 1){ //if photo is in DB more that once then list album names foreach($ar_albums as $name){ echo "A: ".$name['albumName']." <input type='checkbox' name='photoNo[]' value='".$name['photoID']."|".$name['albumId']."|".$name['clientId']."' id='checkbox' /><br>"; } }else{ // if photo is listed only once the list album name foreach($ar_albums as $name){ echo "A: ".$name['albumName']." <input type='checkbox' name='photoNo[]' value='".$name['photoID']."|".$name['albumId']."|".$name['clientId']."' id='checkbox' /><br>"; } } } ?> The second page like so <?php foreach($_POST['photoNo'] as $key => $value) { $ph_ar = explode('|',$value); foreach($ph_ar as $info){ $i++; switch ($i){ case 1: echo "one <br>"; case 2: echo "two <br>"; case 3: echo "three <br>"; $i = 0; default: break; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/#findComment-852959 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2009 Share Posted June 10, 2009 All of that implies that you are executing that posted code inside of a loop that is also using the variable $i and you are also not initializing $i to any value before you are using it in the foreach($ph_ar as $info){ loop. Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/#findComment-852974 Share on other sites More sharing options...
garethhall Posted June 10, 2009 Author Share Posted June 10, 2009 I have now tried many way of writhing this code and still cant get the result I need What am I missing (meaning the switch() ) <?php foreach($_POST['photoNo'] as $key => $value) { $ph_ar = explode('|',$value); $i = 0; foreach($ph_ar as $info){ $i++; switch ($i){ case 1: echo "one <br>"; case 2: echo "two <br>"; case 3: echo "three <br>"; default: break; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/#findComment-853344 Share on other sites More sharing options...
PFMaBiSmAd Posted June 10, 2009 Share Posted June 10, 2009 You might want to read the examples for a switch/case (you are missing break; statements in each case statement) - http://us2.php.net/manual/en/control-structures.switch.php Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/#findComment-853345 Share on other sites More sharing options...
garethhall Posted June 10, 2009 Author Share Posted June 10, 2009 Thanks the break; function sorted it Quote Link to comment https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/#findComment-853395 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.