Foreach mindbender

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

Foreach mindbender

Post by Auselan »

I decided to remove this post as it potentially revealed coding i don't want in the public arena
Last edited by Auselan on Tue Dec 11, 2012 2:54 pm, edited 2 times in total.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: Foreach mindbender

Post by Reviresco »

Not exactly sure what you're trying to do, but here is an example ---

Say you retrieve your two fields, id and contacts, as arrays, and you want to display them as links:

Code: Select all

$query1="SELECT id, contactname FROM contact WHERE practice='$practicename'";
$result1=mysql_query($query1);
 
while ($get_info = mysql_fetch_row($result1)){
 
$id[] = $get_info[0];
$contact[] = $get_info[1];
}
 
 
foreach($contact as $k=>$v) {
 
echo '<a href="example.php?id=' . $id[$k] . '">' . $v . '</a><br />';
}
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

Re: Foreach mindbender

Post by Auselan »

magnificent - thankyou so much dude. with that code it's working almost exactly as I had wanted it to, but still haven't got my brain around it.

The final stumbling block is that it is stacking all contacts now rather than ditching the previous practice contacts as it loops e.g. image where the two contacts for (Not a GP surgery) appear in 19 Beaumont Street's entry as well. Do you know how to do this?
Attachments
Capture.JPG
Capture.JPG (38.86 KiB) Viewed 83 times
User avatar
turbolemon
Forum Commoner
Posts: 70
Joined: Tue Jul 14, 2009 6:45 am
Location: Preston, UK

Re: Foreach mindbender

Post by turbolemon »

You may need to unset the arrays ($id & $content) created with the while loop after each of the "surgeries", as assigning to array[] just adds an element to the end of the list.
Auselan
Forum Newbie
Posts: 18
Joined: Sat Dec 27, 2008 7:04 am

Re: Foreach mindbender

Post by Auselan »

:drunk: thanks a bundle dude. might seem simple, but I'm a total beginner - muchos thankyous guys
Post Reply