homepage Welcome to WebmasterWorld Guest from 54.204.94.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / WebmasterWorld / New To Web Development
Forum Library, Charter, Moderators: brotherhood of lan & mack

New To Web Development Forum

    
How to remove a part of a value or id to insert to mysql
cffrost2




msg:3862823
 7:34 pm on Mar 4, 2009 (gmt 0)

Hi here again.

I am yet on another adventure trying to figure out maybe the impossible ;)

I have a form that calls to a mysql database and returns rows based on criteria.

I have a checkbox added to each row that is numbered each time a row is returned,

for($i=1,$i<=$rownums,$i++) {
$checkboxid = 'use_' . $i;
$remove = 'remove_' . $i;
echo '<td>'.$resultdata['vendor'].'</td>';
echo '<td><input type="checkbox" id="'.$checkboxid.'" value="'.$resultdata['amount'].'" ></t>';
echo '<td><input type="checkbox" id="'.$remove.'" value="'.$resultdata['id'].'" ></t>';
}

I use the incremented checkbox names for use with a javascript function that totals the amounts in a box. I have the second checkbox (not sure if it's needed yet) to auto-check itself to be UPDATED in mysql as "Paid".

How can I do this?
I need to query the database, get the rows, write a checkbox id for each, show a running total of each checkbox checked to show the total amount being paid(and balance against other amounts) and then be able to, in one query, go and update each row to status='paid' and datetime=$datetime WHERE id=$id.

Hope this isn't too much for one post.

Thanks a lot in advance for any light you can shed.
Kevin

 

rocknbil




msg:3863589
 4:37 pm on Mar 5, 2009 (gmt 0)

Well, I'm not sure this would work for you, but given the info in your post,

I need to query the database, get the rows, write a checkbox id for each, show a running total of each checkbox checked to show the total amount being paid(and balance against other amounts) and then be able to, in one query, go and update each row to status='paid' and datetime=$datetime WHERE id=$id.

I am gathering by this snippet,

$resultdata['amount']

That your loop is being output AFTER querying the database. So if you can get at "amount," you can also get at a handle of the record, which we'll call id. If this is the case, you should be using that ID as the iterator, not a generic incrementor in a for loop.

$select = select id, vendor, amount from table where [some condition];


while ($resultdata=mysql_fetch_array($result)) {
$checkboxid = 'use_' . $resultdata['id'];
$remove = 'remove_' . $resultdata['id'];
echo '<td>'.$resultdata['vendor'].'</td>';
echo '<td><input type="checkbox" id="'.$checkboxid.'" value="'.$resultdata['amount'].'" ></td>';
echo '<td><input type="checkbox" id="'.$remove.'" value="'.$resultdata['id'].'" ></td>';
}

so when you go to update (Do not use the raw post variable, cleanse first!:)


// Remember that only checkboxes that are checked
// will show up in $_POST
foreach ($_POST as $key=>value) {
$tag=$id=$select=NULL;
// Since you've placed the record "id" as
// the value here, we don't need to "explode"
// the key as we do below.
if (preg_match("/remove\_\d+/",$key)) {
$select = "delete from purchases where id='$value'";
mysql_query($select);
}
else if (preg_match("/use\_\d+/",$key)) {
// ID is in the key, the value is in ... $value
list ($tag,$id) = explode('_',$key);
$select = "update purchases set amount='$value',datetime=curdate() where id='$id'";
mysql_query($select);
}
}

.... You can either addup your subtotals on the fly or just add a query at the end using sum() to double-check your order subtotals.

cffrost2




msg:3863892
 10:33 pm on Mar 5, 2009 (gmt 0)

Very nice. Thanks. But as you said I'm not sure this will work for me. I am a bit of a beginner to PHP but am a bigtime beginner to Javascript. I'm forced(I think) to have to use javascript to know what fields would print out. (use_1, use_2, use_3... for the javascript to be able to total them.)
function sumChecked () {
var sum = 0;
var sel, elem, ckd;
chk = document.myForm.rownums.value:
sel = 'use_'+i;
elem = document.getElementById(sel);
for(i=1, i<=chk, i++) {
ect.......
In order to maintain an active running total for the employees to know if they can pay out certain items. My problem comes in when processing the selected and changing the status from 'holding' to 'paid' on 'datetime'.

Today I changed the layout, removed the checkboxes(id=use_$i) and created a simple form that prints with each line that is for "click button" to add to the "paid" table, then process based on gathered 'id's. Which works just as well. the page returns the value and works as needed. After they select the receipts to pay, they count their drawer(about 10 fields of counted money), which again keeps a javascripted running total. But if they find they they do not have enough to pay out all the selected to pay, there is another simple form that is to "remove" them from that list and put them pack in the "holding" list(table). When they do that, the "drawercount" form is of course reset and all fields are lost due to the page having to process and refreshing, which, I'm sure you see, would be quite cumbersome.

I would have left it and awaited a response but the owner of the facility is pressing me to have had this done. This is the final piece and in the economy we are in, I don't want to turn the job down. (my first job with this much programming, maybe I should have stuck to html)

HELP! PLEASE!
Have I bitten off more than I can chew here? Am I going the wrong direction? ;) Thanks for everything so far rocknbil.

Kevin

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / WebmasterWorld / New To Web Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved