/    Sign up×
Community /Pin to ProfileBookmark

Help with "foreach" loop

Hi

I have a problem with the following code:

[code=php]
while ($client_contact_row = mysql_fetch_array($client_contact_result)) {
$client_contact[][‘id’][‘data’] = $client_contact_row[‘client_contact_id’];
$client_contact[][‘title’][‘data’] = $client_contact_row[‘client_contact_title’];
$client_contact[][‘first_names’][‘data’] = $client_contact_row[‘client_contact_first_names’];
$client_contact[][‘surname’][‘data’] = $client_contact_row[‘client_contact_surname’];
}

foreach ($client_contact as $client_data){

echo $client_data[‘title’][‘data’];
echo $client_data[‘first_names’][‘data’];
echo $client_data[‘surname’][‘data’];
echo ‘<br>’;
}[/code]

The code works but I only want it to run once every time a new $client_contact_id is reached – something like this(but it don’t work):

[code=php]foreach ($client_contact[‘id’] as $client_data){[/code]

Is it possible in some way?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@SrWebDeveloperOct 13.2010 — but I only want it to run once every time a new $client_contact_id is reached[/QUOTE]

As to your bad code, PHP foreach() takes the parent array name as the argument only, can't pass an index. You nest foreach's when dealing with multidimensional arrays. But your question doesn't correlate with what you are attempting to do via the bad code (my interpretation) so I'm quite confused.

Maybe you mean this (display most recent record in result set, the last "id"):

[code=php]$count=0;
foreach ($client_contact as $client_data){

$count++;
if ($count==count($client_data)) {
echo $client_data['title']['data'];
echo $client_data['first_names']['data'];
echo $client_data['surname']['data'];
echo '<br>';
}
} [/code]


[COLOR="Red"]But that's somewhat weird to do considering you could simply limit the results in the query to the max ID or a "for" instead of foreach and start $counter at count($client_data). See where I'm heading? As to your question, please clarify -- I'm not exactly sure what ya want so we might be heading in different places!!![/COLOR]

-jim
Copy linkTweet thisAlerts:
@dbkauthorOct 13.2010 — Hi SrWebDeveloper

Thanks for your reply!

What I want is this:

Make a query that gets a unknown numbers of [I]contacts[/I] and all the information on every [I]contact[/I].

Then I want to place the result in a multidimensional array created with a while loop:
[code=php]while ($client_contact_row = mysql_fetch_array($client_contact_result)) {
$client_contact[]['id']['data'] = $client_contact_row['client_contact_id'];
$client_contact[]['title']['data'] = $client_contact_row['client_contact_title'];
$client_contact[]['first_names']['data'] = $client_contact_row['client_contact_first_names'];
$client_contact[]['surname']['data'] = $client_contact_row['client_contact_surname'];
}[/code]


Now that I got all the information in a multidimensional array I want to generate a html form for each [I]contact[/I], where the input fields value is the content of the array. That's why I want it to loop every time et hits the [I]contact_id[/I].

Hope the explanation is understandably!
Copy linkTweet thisAlerts:
@SrWebDeveloperOct 13.2010 — The part of your original question that threw me off was your use of the word "once". Based on your recent explanation:

[code=php]for($count==0; $count<count($client_contact); $count++) {
echo "Client ID={$client_contact[$count]['id']['data']}<br />";
echo "Title={$client_contact[$count]['id']['title']}<br />";
echo "First Name={$client_contact[$count]['id']['first_names']}<br />";
echo "Surname={$client_contact[$count]['id']['surname']}<br /><br />";
}
[/code]


It should be noted normally I'd iterate through the result set directly, not copy the data into my own multidimensional array and iterate through that.

If this isn't what you meant, I'm going to admit I'm totally stupified (not your fault) and ask for someone else reading this to send me a life line.

:p

-jim
Copy linkTweet thisAlerts:
@dbkauthorOct 13.2010 — Hi SrWebDeveloper

I can't make your code work the way I would like.

But thanks for all your help anyway!
Copy linkTweet thisAlerts:
@SrWebDeveloperOct 13.2010 — I want to generate a html form for each [I]contact[/I], where the input fields value is the content of the array[/QUOTE]

My code successfully iterates through your multidimensional array so the id, title, first name and surname for each row ("contact") from the original result set are embedded in HTML. I posted it so you could see how to reference the variable data in HTML. The for loop processes each row.

It's up to you to wrap the proper HTML around the variables, i.e. add a form tag, set form fields, default values, etc.

But simply saying "you can't make it work" is vague if you need further assistance from others (no offense intended). I appreciate your time and patience, and wish you the best.

-jim
Copy linkTweet thisAlerts:
@dbkauthorOct 13.2010 — Hi SrWebDeveloper

I understand perfectly!

I just have to try out different ways to reach the goal - maybe your solution is the way.

Thanks for your time!
×

Success!

Help @dbk 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.24,
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,
)...