Forum Moderators: coopster

Message Too Old, No Replies

Splitting string from mysql and linking each word.

         

dkin

9:10 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



I have a submission form on my website with about 150 checkboxes. When these checkboxes are entered into the database they are entered as a solid string, therefore when they are extracted from the database they are the same.

I would like to split this string into individual strings.

When it is entered into the db it is entered like this. widget, widget 1, widget 2, etc, etc.

So I would like it to be split at each comma if possible then linked to the corresponding widget page.

Does anyone know how to do this?

coopster

9:18 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



explode() [php.net] may be an option for you.

dkin

9:31 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



I have tried explode but can not quite figure out how to use it.

Will try again and post results.

Birdman

10:34 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It would go something like this:

while ($i = mysql_fetch_array(mysql_query("select..."))){

$options = explode($i['field_name_here']);

foreach ($options as $option)
print "<a href=\"/folder/".trim($option).".php\"></a>";

dkin

11:31 pm on Jul 27, 2004 (gmt 0)

10+ Year Member



I do not completely understand that.

while ($i = mysql_fetch_array(mysql_query("select..."))){

$options = explode($i['field_name_here']);

foreach ($options as $option)
print "<a href=\"/folder/".trim($option).".php\"></a>";

while ($i = mysql_fetch_array(mysql_query("select..."))){

is that needed or could I just put

while ($i = $myrow['row']){

?

Birdman

11:36 pm on Jul 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure, you can get your db results any way you want to. As long as you get the comma separated string into the explode function, you're good to go.

$options = explode(comma separated string);

foreach ($options as $option)
print "<a href=\"/folder/".trim($option).".php\"></a>";

dkin

2:37 am on Aug 3, 2004 (gmt 0)

10+ Year Member



ok I have converted Birdmans code into this.

$options = explode($myrow['Zones']);

foreach ($options as $option)
print "<a href=\"/modules.php?name=Zone_Database&file=zone_detail&id=$myrow[id]/".trim($option)."\">".trim($option)."</a>";

and am getting two errors.

Wrong parameter count for explode()on line 49 which is the defining of options line and

Invalid argument supplied for foreach() on line 51 which is the foreach line.

Any ideas?

I will keep playing with it and look up both errors on php.net but if anyone has any feedback it would be greatly appreciated. Thanks

Dylan

dkin

3:44 am on Aug 3, 2004 (gmt 0)

10+ Year Member



**UPDATE**

This is my code after tampering with it.

$zone = $myrow['Zones'];
$options = explode(" ", $zone);
foreach ($options as $option)
print "<a href=\"modules.php?name=Zone_Database&file=zone_detail&id=".$myrow[id]."\">".($option)."</a>";

it is not fully operational but I am getting feedback now. But it is splitting everything into a link even if it is a word such as "Whale Hill" that turns to 2 links.

and I am not getting spaces after the commas but I am sure I can figure that out.

Birdman

3:51 am on Aug 3, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, my fault. I gave you bad code for the explode function. You almost got it right in your last example, except I think you need use a comma as the separator.

explode(",", $zone);

And if you want lose the extra space after the comma, just add it to the separator string.

explode(", ", $zone); //be sure there will always be a space!

dkin

5:45 am on Aug 3, 2004 (gmt 0)

10+ Year Member



k I got all that working fine.

Now I am trying to link to the related zones from the quest database.

This is the link I have

<a href=\"modules.php?name=Zone_Database&file=zone_detail&name=$option\">$option</a>

It looks perfect but I get

Sorry, such file doesn't exist...

When I click the link.

I recently changed the link structure from $id to $name, as soon as I did that I started getting this error.

Any ideas?

dkin

7:16 am on Aug 4, 2004 (gmt 0)

10+ Year Member



anyone know what I can do?

Birdman

12:53 pm on Aug 4, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the file doesn't exist, either the path that is wrong or you are using a header() redirect in that script. Look for the header redirect and change the variable there to the new one youo are using.

dkin

9:05 pm on Aug 4, 2004 (gmt 0)

10+ Year Member



This is the link that I am using.

do {
print "<tr><td align=\"center\">";
echo '<b><a href="modules.php?name=$module_name&file=zone_detail&name=', $myrow['name'] ,'">', $myrow['name'] ,'</a></b>';
print "<br></td></tr>";

}

I need to link by name so that I can link from my quest database as well.

but I am still getting this "Sorry, such file doesn't exist..."

this is a link when clicked.

modules.php?name=$module_name&file=zone_detail&name=Broken%20Skull%20Rock

any suggestions.

jatar_k

9:26 pm on Aug 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what is the root relative path of that file?

dkin

9:36 pm on Aug 4, 2004 (gmt 0)

10+ Year Member



I am always confused by that but I think this is what you are asking for.

/usr/local/bin/php /home/eqoagui/public_html/modules/Quest_Database/quest_detail.php

and

/usr/local/bin/php /home/eqoagui/public_html/modules/Quest_Database/index.php

jatar_k

9:43 pm on Aug 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



actually i was thinking from a web perspective, sorry for my lack of clarity

if someone types in www.yoursite.com
then the main page of your site would be in
/usr/local/bin/php/home/eqoagui/public_html/

so the root relative path for the file you want to link to would be

/modules/Quest_Database/quest_detail.php
or
/modules/Quest_Database/index.php

why are you trying to link to modules.php? Which is the actual file, of these three, that you want the generated link to go to?

dkin

11:35 pm on Aug 4, 2004 (gmt 0)

10+ Year Member



I am linking to modules.php because that is the format of all links when using php nuke.

I would like the link to go from The index.php in the zone database and the item_detail.php in the item database to zone_detail.php$name=$zone

Now I looked at my address a minute ago and saw that there is?name=$module_name now if I have $name=$zone_name in there as well would?name and $name conflict, could that be causing my problems, because I have made many modules and this problem has never lasted this long.

Cheers Dylan

jatar_k

11:38 pm on Aug 4, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



hmmm

something I didn't notice before

echo '<b><a href="modules.php?name=$module_name&file=zone_detail&name=', $myrow['name'] ,'">', $myrow['name'] ,'</a></b>';

that won't resolve the variable $module_name because it is between single quotes. Just pop it out of the single quotes like so

echo '<b><a href="modules.php?name=',$module_name,'&file=zone_detail&name=', $myrow['name'] ,'">', $myrow['name'] ,'</a></b>';

dkin

12:24 am on Aug 5, 2004 (gmt 0)

10+ Year Member



I changed module_name to Zone_Database which is the name of the module, but still getting the same problem.

Could it be the variables in the address conflicting?

Birdman

1:27 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to open modules.php and see what is going on. That's where the problem is.

If you're getting "No such file exists", then there is obviously a function that sends a 404 redirect if the data isn't on the up-and-up. Since you changed the link format, you upset the script because it's expecting id, not name.

dkin

3:20 am on Aug 5, 2004 (gmt 0)

10+ Year Member



I quickly looked through modules.php and found nothing so I changed $name in my address to zone id.

I am now getting this error

"query 1: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'Cave' at line 1"

this is the address

"/modules.php?name=Zone_Database&file=zone_detail&zoneid=Bear%20Cave"

and this is the sql

$results = mysql_query("SELECT * FROM nuke_zones WHERE name=$zoneid",$link) or die ("query 1: " . mysql_error());
$myrow = mysql_fetch_array($results) or die ("query 2: " . mysql_error());

Birdman

3:25 am on Aug 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a string, so it needs to be surrounded with quotes.

SELECT * FROM nuke_zones WHERE name='$zoneid'

dkin

4:12 am on Aug 5, 2004 (gmt 0)

10+ Year Member



GOT IT!

changed that and altered a few variables and its bang on, Thanks all.

Think I might be starting to understand this php thing.

Cheers