Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with Arrays and foreach loop please!!!
Old 06-15-2011, 03:45 AM Help with Arrays and foreach loop please!!!
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
Hello,

i have a page where it shows the current records which status is active usually there are like 30 records active, every records has a check box so you can select it i have made a button so you can print the selected records the user can only select a max of 4 records and the code works fine but if they select less than 4 records the code wont work heres the code.

PHP Code:
foreach($keys as $idx=>$val)
{
    
$id1 $keys[0]["case_#"];
    
$id2 $keys[1]["case_#"];
    
$id3 $keys[2]["case_#"];
    
$id4 $keys[3]["case_#"];
}

$result["txt"] = $params["url1"]."id1=".$id1."&id2=".$id2."&id3=".$id3."&id4=".$id4;

// this works fine if they select 4 records to print but it doesnt work if they select less than 4. $params["url1"] contans the page example.page.php? i want to pass the ids through the url so i can get them and then print the page 
Please help if you need to see more code let me know but i think theres a better way to do this with the loop functions of php i just dont have that much experience with PHP thanks
stivens is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 06-15-2011, 06:01 AM Re: Help with Arrays and foreach loop please!!!
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
PHP runs on the server so only runs when the form is submitted, use javascript to disable the submit button if the selected count is not four.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 06-15-2011, 07:27 AM Re: Help with Arrays and foreach loop please!!!
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
Your loop doesn't really make sense. You aren't using the $idx and $val variables, so the loop will just do the exact same operation every time. You're probably trying to do something like this?

PHP Code:
$result['txt'] = $params['url1'];
foreach (
$keys as $id => $val) {
   
$result['txt'] .= 'id' $id '=' $val '&';
}
// Trim off the last &
rtrim($result['txt'], '&'); 
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Old 06-15-2011, 10:21 AM Re: Help with Arrays and foreach loop please!!!
Skilled Talker

Posts: 96
Name: Joan
Trades: 0
Quote:
Originally Posted by lizciz View Post
Your loop doesn't really make sense. You aren't using the $idx and $val variables, so the loop will just do the exact same operation every time. You're probably trying to do something like this?

PHP Code:
$result['txt'] = $params['url1'];
foreach (
$keys as $id => $val) {
   
$result['txt'] .= 'id' $id '=' $val '&';
}
// Trim off the last &
rtrim($result['txt'], '&'); 
YES!!!!!! this is what i was looking for thank you soo much!!!! yes this was my first time using loops but this works great i just had to do a little modification to it. your code output this:

pdf_folder/page_print.php?id0=Array&id1=Array&

i didnt write the script on the page so i'm not so sure how it works but i know for each $keys[] it holds each row's value so i just need my id value ie. if i need the id they selected i would refer to it like this $val['case_#'] if i need it the first name i would do this $val['first_name'] and so on...

so i modified the code like this

PHP Code:
$result['txt'] = $params['url1']; 
foreach (
$keys as $id => $val) { 
   
$result['txt'] .= 'id' $id '=' $val['case_#'] . '&'

// Trim off the last & 
$fresult rtrim($result['txt'], '&');
$result['txt'] = $fresult
Thanks for your help greatly appreciate it!!

Last edited by stivens; 06-15-2011 at 02:18 PM..
stivens is offline
Reply With Quote
View Public Profile
 
Old 06-16-2011, 05:40 AM Re: Help with Arrays and foreach loop please!!!
lizciz's Avatar
Super Spam Talker

Posts: 807
Name: Mattias Nordahl
Location: Sweden
Trades: 0
You're welcome.
Oh, and you do not need that extra variable $fresult, you can replace those last two rows with this:
PHP Code:
$result['txt'] = rtrim($result['txt'], '&'); 
__________________
Your answers will only be as good as your question. Formulate it well and give all the necessary information.
lizciz is online now
Reply With Quote
View Public Profile Visit lizciz's homepage!
 
Reply     « Reply to Help with Arrays and foreach loop please!!!
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.52212 seconds with 12 queries