Jump to content

Recommended Posts

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 />";
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/161617-can-a-check-box-store-2-values/
Share on other sites

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.

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

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;
	}
}

}
?>

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.

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;
	}
}
}
?>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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