Linking stuff

This is a discussion on Linking stuff within the PHP General forums, part of the PHP Programming Forums category; Hi all. I think I might be having a brain-fart afternoon, but I can't think of how to ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-08-2008
Philip Thompson
 
Posts: n/a
Default Linking stuff

Hi all.

I think I might be having a brain-fart afternoon, but I can't think of
how to accomplish this.

I want these "centers" to be linked to 1 another. If center A links to
B and C, I want B to be linked to A and C, and C to A and B. So... it
should look something like this:

center link
A B
A C
B A
B C
C A
C B

These results will be stored in a database and each of the records is
unique. To slightly simplify things (hopefully!), I can just store
each of the centers in an array, like so: array(A,B,C,D). I figure
I'll have to loop through the array (several times?) and do
comparisons with each other one.

Basically, I don't want to over-complicate this for myself. Any
guiding light would be appreciated on how to link all of these!

Thanks
~Philip
Reply With Quote
  #2 (permalink)  
Old 07-08-2008
Børge Holen
 
Posts: n/a
Default Re: [PHP] Linking stuff

On Tuesday 08 July 2008 21:22:34 Philip Thompson wrote:
> Hi all.
>
> I think I might be having a brain-fart afternoon, but I can't think of
> how to accomplish this.
>
> I want these "centers" to be linked to 1 another. If center A links to
> B and C, I want B to be linked to A and C, and C to A and B. So... it
> should look something like this:
>
> center link
> A B
> A C
> B A
> B C
> C A
> C B
>
> These results will be stored in a database and each of the records is
> unique. To slightly simplify things (hopefully!), I can just store
> each of the centers in an array, like so: array(A,B,C,D). I figure
> I'll have to loop through the array (several times?) and do
> comparisons with each other one.
>
> Basically, I don't want to over-complicate this for myself. Any
> guiding light would be appreciated on how to link all of these!


Basicly you can use if to check similarities else link.

>
> Thanks
> ~Philip




--
---
Børge Holen
http://www.arivene.net
Reply With Quote
  #3 (permalink)  
Old 07-08-2008
tedd
 
Posts: n/a
Default Re: [PHP] Linking stuff

At 2:22 PM -0500 7/8/08, Philip Thompson wrote:
>Hi all.
>
>I think I might be having a brain-fart afternoon, but I can't think
>of how to accomplish this.
>
>I want these "centers" to be linked to 1 another. If center A links
>to B and C, I want B to be linked to A and C, and C to A and B.
>So... it should look something like this:
>
>center link
>A B
>A C
>B A
>B C
>C A
>C B
>
>These results will be stored in a database and each of the records
>is unique. To slightly simplify things (hopefully!), I can just
>store each of the centers in an array, like so: array(A,B,C,D). I
>figure I'll have to loop through the array (several times?) and do
>comparisons with each other one.
>
>Basically, I don't want to over-complicate this for myself. Any
>guiding light would be appreciated on how to link all of these!
>
>Thanks
>~Philip


It looks like a permutation and sounds like a linked list.

Maybe it would be better if you explained what you wanted to do wit it.

Cheers,

tedd

--
-------
http://sperling.com http://ancientstones.com http://earthstones.com
Reply With Quote
  #4 (permalink)  
Old 07-08-2008
Shawn McKenzie
 
Posts: n/a
Default Re: Linking stuff

Philip Thompson wrote:
> Hi all.
>
> I think I might be having a brain-fart afternoon, but I can't think of
> how to accomplish this.
>
> I want these "centers" to be linked to 1 another. If center A links to B
> and C, I want B to be linked to A and C, and C to A and B. So... it
> should look something like this:
>
> center link
> A B
> A C
> B A
> B C
> C A
> C B
>
> These results will be stored in a database and each of the records is
> unique. To slightly simplify things (hopefully!), I can just store each
> of the centers in an array, like so: array(A,B,C,D). I figure I'll have
> to loop through the array (several times?) and do comparisons with each
> other one.
>
> Basically, I don't want to over-complicate this for myself. Any guiding
> light would be appreciated on how to link all of these!
>
> Thanks
> ~Philip


Well, it depends upon what you want to do with these. You'll have them
in a db, so it's easy to get them out as an array. What's the problem?

-Shawn
Reply With Quote
  #5 (permalink)  
Old 07-08-2008
Philip Thompson
 
Posts: n/a
Default Re: [PHP] Re: Linking stuff

On Jul 8, 2008, at 3:38 PM, Shawn McKenzie wrote:

> Philip Thompson wrote:
>> Hi all.
>> I think I might be having a brain-fart afternoon, but I can't think
>> of how to accomplish this.
>> I want these "centers" to be linked to 1 another. If center A links
>> to B and C, I want B to be linked to A and C, and C to A and B.
>> So... it should look something like this:
>> center link
>> A B
>> A C
>> B A
>> B C
>> C A
>> C B
>> These results will be stored in a database and each of the records
>> is unique. To slightly simplify things (hopefully!), I can just
>> store each of the centers in an array, like so: array(A,B,C,D). I
>> figure I'll have to loop through the array (several times?) and do
>> comparisons with each other one.
>> Basically, I don't want to over-complicate this for myself. Any
>> guiding light would be appreciated on how to link all of these!
>> Thanks
>> ~Philip

>
> Well, it depends upon what you want to do with these. You'll have
> them in a db, so it's easy to get them out as an array. What's the
> problem?
>
> -Shawn


Ok, I figured out one solution. My brain finished farting. Here's what
I came up with:

<?php
$center_ids = explode(",", $_POST["list"]);
$count = count ($center_ids);
$list = array();

for ($i=0; $i<$count; $i++) {
for ($j=0; $j<$count; $j++) {
if ($j != $i && !in_array($center_ids[$j],
$list[$center_ids[$i]])) {
$list[$center_ids[$i]][] = $center_ids[$j];
}
}
}

foreach ($list as $cid1 => $array) {
foreach ($array as $cid2) {
$db->query("INSERT INTO `center_link` (`center_id`,
`link_center_id`) VALUES ('$cid1', '$cid2')");
}
}
?>

The resulting array for $list (if centers 1, 2 and 3 are selected) is:

Array
(
[1] => Array
(
[0] => 2
[1] => 3
)

[2] => Array
(
[0] => 1
[1] => 3
)

[3] => Array
(
[0] => 1
[1] => 2
)
)

If there's a cleaner/better way, feel free to set me straight. ;)

Thanks for your input, guys.

~Phil
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:53 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0