Stupid MYSQL/PHP Question

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
I'm using

Code:
$IDs=mysql_result($result,$i,"ID(s)");

and then echoing $IDs for each...

However, the info is stored in the table as xxx,xxy,xxz,xzz for the IDs..... I need to somehow strip this as each xxx is a variable for an url.

I.e. when I echo, I want to echo xxx,xxy,xxz,xzz where each individual entry has an url (e.g. www.url.com/xxx etc..).

Why can't I figure this out? :S
 
Beansprout said:

Blast it! That's the one... :)

How do I explode it, however when the number of xxx's varies per row?

I have:

Code:
$splitdata = explode(',', $PMIDs);
$totaldata = count($splitdata);
but obviously can only display $splitdata[0]; $splitdata[1]; so on if I know how many there are...

i.e. have:

Code:
<td><a href=\"http://url.com/$splitdata[0]\">$splitdata[0]</a>, <a href=\"http://url.com/$splitdata[1]\">$splitdata[1]</a>, <a href=\"http://url.com/$splitdata[2]\">$splitdata[2]</a></td>
If there were only 3 in that field... WHat if there were just the one? or 25?
 
Last edited:
jdickerson said:
I'm using

Code:
$IDs=mysql_result($result,$i,"ID(s)");

and then echoing $IDs for each...

However, the info is stored in the table as xxx,xxy,xxz,xzz for the IDs..... I need to somehow strip this as each xxx is a variable for an url.

I.e. when I echo, I want to echo xxx,xxy,xxz,xzz where each individual entry has an url (e.g. www.url.com/xxx etc..).

Why can't I figure this out? :S
redesign your data structure to have a new table for id's with a one to many foreign key for whatever it is they relate to.
 
Dj_Jestar said:
redesign your data structure to have a new table for id's with a one to many foreign key for whatever it is they relate to.

Don't exactly understand you.... and the data in the table is imported from premade csvs... so changing the structure might be a pain....

Perhaps I'm being unclear... I have in the field a set of numbers. These numbers are unique IDs, however they're grouped together e.g. for simplicity, "1,2,3,4,5" Now this is great for me as the site they link to can read and link them, so just doing a link to url.com/1,2,3,4,5 works perfectly. However, if they're are so many of them, e.g. url.com/1,2,3,4,5,6....,27 the site can't handle this so throws a bobbly. I therefore want to have each 'number' with a seperate link...

So 1(url.com/1), 2(url.com/2), 3(url.com/3)... and so on... but the data is stored in a single field per row as 1,2,3,4,5... still.

That wasn't any clearer, was it... :S
 
Code:
+-------------------------+
| id | foreign_id | value |
+-------------------------+
| 1  | 98765      | 1132  |
| 2  | 98765      | 13125 |
| .. | ...        | ...   |
| 3  | 98764      | 12345 |
+-------------------------+

then..
Code:
<?php

$result = mysql_query("SELECT `value` FROM `ids` WHERE `foreign_id` = '$idFromFirstTable'");

$url = 'http://www.example.com';

while ($row = mysql_fetch_assoc($result))
{
  foreach ($row as $var => $val)
  {
    $url .= "/$var/$val"; // change to whatever format.
  }
}

?>
 
Last edited:
Dj_Jestar said:
Code:
+-------------------------+
| id | foreign_id | value |
+-------------------------+
| 1  | 98765      | 1132  |
| 2  | 98765      | 13125 |
| .. | ...        | ...   |
| 3  | 98764      | 12345 |
+-------------------------+

then..
Code:
<?php

$result = mysql_query("SELECT `value` FROM `ids` WHERE `foreign_id` = '$idFromFirstTable' LIMIT 1");

$url = 'http://www.example.com';

foreach(mysql_fetch_assoc($result) as $var => $val)
{
    $url .= "/$var/$val"; // change to whatever format.
}

?>

ID is just an arbitary name (in fact it is pid) not the index of the row....
 
hence the column "value"..

the id field is an auto incremented number used as a unique identifier for each record (for mysql internal indexing)


EDIT: just noticed my php code is wrong, adjusting now.. done.
 
Back
Top Bottom