/    Sign up×
Community /Pin to ProfileBookmark

php check boxes

below is code that displays a cecK box next to each item found. I’m trying to write code that will allow the user to select all that apply and click the submit button and update the table next to the correct entry in the table

I’m having hard time getting it to work. any help is great

[code=php]

// updateTABLE
$update_buddy = mysql_query(“UPDATE buddylink SET status= ‘$status’, relative = ‘$relatives’ WHERE buddy_id = ‘$id'”)or die (“Link Create Error:” .mysql_error());

}

$last_login = $_REQUEST[‘last_login’];

$query = (“SELECT * FROM users WHERE user_id = ‘$user_id'”) or die .mysql_error();

$result=mysql_query($query)or die(“Get Search results Error: “.mysql_error());
while($r=mysql_fetch_assoc($result)) {

$last_login = $r[‘last_login’];
}
[/code]

[code=php]

<?php
$get_buddy_request = (“SELECT * FROM buddylink WHERE buddy_id = ‘$user_id’ AND status = ‘1’”);
$buddy_request=mysql_query($get_buddy_request)or die(“get_buddy_request Search results Error: “.mysql_error());

while($row_buddy_request = mysql_fetch_assoc($buddy_request)){
$requester_id = $row_buddy_request[‘owner_id’];

$get_buddy_request_info = (“SELECT * FROM users WHERE user_id = ‘$requester_id'”);
$buddy_request_info = mysql_query($get_buddy_request_info)or die(“get_buddy_request_info Search results Error: “.mysql_error());

while($row_buddy_request_info = mysql_fetch_assoc($buddy_request_info)){
$id = $row_buddy_request_info[‘user_id’];
?>

<tr><td class=”rowRight”><a href=”SearchDetails.php?owner_id=<?php echo $id;?>”><?php echo $row_buddy_request_info[‘first_name’].” “. $row_buddy_request_info[‘last_name’]; ?></a> </td>
<td class=”rowRight”><input name=”related[]” type=”checkbox” id=”related” value=”true”></td>
<td class=”rowRight”>
<input name=”approve[]” type=”checkbox” id=”approve” value=”2″>
<input type=”hidden” name=”id” value=”<?php echo $row_buddy_request_info[‘buddy_id’]; ?>”>
</td>

</tr>

<?php }
}?>
[/code]

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@TyreeDec 08.2006 — You're doing the right thing by setting the name of each checkbox as an array.

I'd just change them to look like this: related[$id]

That way each id is placed in the array.

After you have your array, it's a simple matter to run through it and perform your db operations.

foreach ($related AS $relative){

SQL query to insert $relative into the db table

}
Copy linkTweet thisAlerts:
@kprocauthorDec 08.2006 — I was trying to assign each id to a new array, from a hidden field but that does not work, If i under standard what you posted correctly by putting related[$id] for the check box name. it tags the array with the value of the variable $id

I'm I thinking correctly

thank you for the help
Copy linkTweet thisAlerts:
@TyreeDec 08.2006 — You got it! That way you can match the id in the database with the id contained in the array. The $id put into the array would be the key and "true" would be the value, if checked.

Thinking of that...I think your foreach statement should actually be:
[code=php]
foreach ($related AS $key=>$value) {
if ($value == "true") {
$relID = $key;
SQL Query...compare $relID to the id in the database
}
}
[/code]
Copy linkTweet thisAlerts:
@kprocauthorDec 08.2006 — I'm running into another little snag

If i leave on check box un checked that lys within the foreach statement then i get error


Warning: Invalid argument supplied for foreach() in C:Program FilesxampphtdocsfamilyclickHome.php on line 22

here is what I have this far

code that will eventually update table, still trying to figure out how to bring it all together.
[code=php]

if(isset($_POST['submit_approve'])) {

foreach($_POST['approve'] as $key=> $status) {
echo $status;
}


foreach($_POST['related'] as $key=> $relative) {
echo $relative;
}


// update TABLE
$update_buddy = mysql_query("UPDATE buddylink SET status= '$status', relative = '$relatives' WHERE buddy_id = '$id'")or die ("Link Create Error:" .mysql_error());

}
[/code]



creating the table
[code=php]

<table class="columnTable" cellspacing="2">
<form name="approval" method="post" action="<?php $PHP_SELF; ?>">
<tr><th class='tableheader' colspan="3">Buddy Link Requests</th></tr>
<tr><td class="rowLeft">Name</td>
<td class="rowRight">Family</td>
<td class="rowRight">Approve</td></tr>

<?php
$get_buddy_request = ("SELECT * FROM buddylink WHERE buddy_id = '$user_id' AND status = '1'");
$buddy_request=mysql_query($get_buddy_request)or die("get_buddy_request Search results Error: ".mysql_error());

while($row_buddy_request = mysql_fetch_assoc($buddy_request)){
$requester_id = $row_buddy_request['owner_id'];
$id = $row_buddy_request['buddy_id'];

$get_buddy_request_info = ("SELECT * FROM users WHERE user_id = '$requester_id'");
$buddy_request_info = mysql_query($get_buddy_request_info)or die("get_buddy_request_info Search results Error: ".mysql_error());


while($row_buddy_request_info = mysql_fetch_assoc($buddy_request_info)){
$id = $row_buddy_request_info['user_id'];
?>

<tr><td class="rowRight"><a href="SearchDetails.php?owner_id=<?php echo $id;?>"><?php echo $row_buddy_request_info['first_name']." ". $row_buddy_request_info['last_name']; ?></a> </td>
<td class="rowRight"><input name="related[<?php echo $id; ?>]" type="checkbox" id="related" value="Yes"></td>
<td class="rowRight">
<input name="approve[]" type="checkbox" id="approve" value="2">
<input type="hidden" name="id[<?php echo $id; ?>]" value="<?php echo $id; ?>">
</td>

</tr>

<?php }
}?>
<tr><td><input type="submit" name="submit_approve" value="Submit">
</tr></td>
</form>
</table>

[/code]
Copy linkTweet thisAlerts:
@TyreeDec 09.2006 — Okay, I've been looking at your code and trying to figure out exactly what it is you're trying to do. I think I have a pretty good idea, but let me run it past you and you tell me where I'm off.

I'm assuming someone has logged in at this point and you have some sort of user id. Looks like the var for the logged in user is $user_id.

Looks like you have 2 db tables, buddy_link and users. buddy_link stores links between buddies and users stores all the users (buddies).

buddy_link table:

I count at least 4 fields, buddy_id, status, relative and owner_id.

Okay, here's wher I start getting a bit confused.

It appears that owner_id is a requester of a link with buddy_id. the status, if set to '1' means a request has been made, if set to '2' the request has been approved by buddy_id ($user_id).

Then there's the relative field...I'm assuming this can be set by either user, right?

If this is truly the way this works. It seems that you'd have a lot of redundant info, wouldn't you? I mean, say the current user also wanted to request buddy-ship from the current requester. Then you'd have two rows in the table, one for each direction. And then if a user had 50 buddies...well, then there's 100 rows going both directions.

I may have things totally mixed up. I just want to make sure I understand your goal before I start spouting off suggestions! ?

Moving forward...So, when the user visits this page, he's presented with all the buddies who have requested a llink with him. He can then check approve for each one, if he desires. He can also select whether each one is a relative or not??? Wouldn't the requester have already done this? Or could they possibly have already done this? If so, what then?


If all you want me to do is tell you how to collect the info from both checkboxes (relative and approve) and insert them into the buddy_link table, just say so and I'll stop trying to figure out your grand scheme! ?
Copy linkTweet thisAlerts:
@TyreeDec 09.2006 — Just in case...I figured I'd work something up for you:

http://tyreeonline.com/tests/check_boxes/index.php

So, that's a working example of how you could handle this.

Here's the code for the listing of the buddy requests:
[code=php]
<form name="getBuddies" action="<?php print $PHP_SELF; ?>" method="post" >
<table width="350" border="1" cellspacing="0" cellpadding="3">
<?php foreach ($buddies AS $buddy){ ?>
<tr>
<td width="100"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td><input type="checkbox" name="related[]" value="<?php print $buddy['ID']; ?>" />
Is Buddy</td>
</tr>
<tr>
<td><input type="checkbox" name="approve[]" value="<?php print $buddy['ID']; ?>" /> Approve</td>
</tr>
</table>
</td>
<td colspan="2" align="right"><table width="100%" border="0" cellspacing="0" cellpadding="3">
<tr>
<td align="right">Buddy ID: </td>
<td bgcolor="#CCCCCC"><?php print $buddy['ID']; ?></td>
</tr>
<tr>
<td align="right">First Name: </td>
<td bgcolor="#CCCCCC"><?php print $buddy['first_name']; ?></td>
</tr>
<tr>
<td align="right">Last Name: </td>
<td bgcolor="#CCCCCC"><?php print $buddy['last_name']; ?></td>
</tr>

</table></td>
</tr>

<?php } ?>
</table>
<input type="submit" name="Submit" value="Process Buddies" />
</form>
[/code]



You can see that I changed things a bit. I set the value of each checkbox to the requester's ID instead of putting the requester's ID as the key of the array.

Here's the code for processing the arrays created by the checkboxes:
[code=php]
if (is_array($approve) || is_array($related)){

if (is_array($approve)) {
print "Request Approvals:<br/>";
foreach ($approve AS $key=>$value){
print "Requester ID: ".$value."<br/>" ;
print "Query: UPDATE buddy_link SET status=2 WHERE buddy_id=$user_id AND owner_id=$value"."<br/>";
}
print "<br /><br />";
} else {
print "You approved no requests.<br /><br />";
}

if (is_array($related)) {
print "Selected Relatives:<br/>";
foreach ($related AS $key=>$value){
print "Requester ID: ".$value."<br/>" ;
print "Query: UPDATE buddy_link SET relative='Yes' WHERE buddy_id=$user_id AND owner_id=$value"."<br/>";
}
} else {
print "You selected no relatives.";
}

}
[/code]



Now, this is the code I used for the example page to just print out the queries. You'll need to use elements of it, but not all the code. You'll basically just use the foreach statements to run the query for each requester's ID.

Hope this helps!
Copy linkTweet thisAlerts:
@kprocauthorDec 09.2006 — thank you for the help and giving an excellent example. I checked out your website very nice. ?
Copy linkTweet thisAlerts:
@TyreeDec 09.2006 — So, did you get it figured out then?
Copy linkTweet thisAlerts:
@kprocauthorDec 09.2006 — all is working excellent, thank you
Copy linkTweet thisAlerts:
@TyreeDec 10.2006 — Good deal! That's awesome! You're most welcome!
×

Success!

Help @kproc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...