making hyperlinks display as concat

Easiest explanation is to look here: http://wmomusic.org/fl1/flpets5.php

You can see that the abilities "Location"and “skills” have multiple results. Those are group-concatted into display into displaying in 1 cell and named LOCidlist and Skillidlist.

So far so good. But as you can see making them display in the cell using echo with <a href:" > makes every word in that cell the hyperlink corresponding to the first result. Which works fine if the result is only 1 location or skill.

but of course this does not work if there are multiple results.

So somehow i need to 1) either make each word a hyperlink BEFORE group concatting them

  1. or somehow tell the echo command to link every word to the corresponding location id or skill id.

I have no idea how to go at this. My first thought was to create an array, as hyperlink and then use concat. (option 1) …but i read that won’t work…

anyone has encountered this problem and how did you solve it? I have over 700 results in my tables so it needs to be programmable (not manual entries)

And the code you are using to create the links is where?

For some reason, your page will not open in my browser. But, anyway, for links, which are anchors, you can
place anything inside the tags. Quite often, you have an image with text and want it all anchored to a
page. That is easy. Basically, anything between the tags activate that one link… So, loosely…
some text
Some more text
< h2>Some header
further text

Any of that will trigger the link. So, if you need to include more into the link, just go ahead and do it. Also, if
you only wanted parts of the above to go to the same place, you can use multiple anchors that have the same
referrence for parts if needed. So, you could use the same anchors more than once.
Hope that helps some…

Hi! thanks for the replies. yes that puts me in the direction i think, but still unsure how to do it… Here is the coding: (maybe from this you can also see why you can not open the page on internet? So far I could open it myself in mozilla, chrome, IE11, safari, konqueror)

As you can see I now grouped the skills and locations as a list and further on echo this list as hyperlink…that is where it goes wrong. Which makes perfect sence to me, just don’t see how to correct that… :slight_smile: Thanks in advance for any hints!

[php]

<?php include("../flconnect.php"); $query="SELECT pets.PET_ID, pets.Petname, pets.HP, pets.str, pets.dex, pets.luck, pets.con, elementgrowth.EL_growth, pets.charm, pets.int, pets.learningslots, pets.growth, pets.Levelmin, pets.Levelmax, skills.function, elements.elementname, pets.imageurl, locations.URLtomap, GROUP_CONCAT(DISTINCT locations.Location ORDER BY locations.Location SEPARATOR ', ') AS locidlist, GROUP_CONCAT(DISTINCT skills.skillname ORDER BY skills.skillname SEPARATOR ', ') AS skilllist FROM pets JOIN petlocations ON petlocations.PET_ID=pets.PET_ID JOIN locations ON petlocations.LOC_ID=locations.LOC_ID JOIN elements ON pets.element_id=elements.element_id JOIN petskill ON pets.PET_ID=petskill.Pet_ID JOIN skills ON petskill.Skill_ID=skills.SKILL_ID JOIN elementgrowth ON elements.element_id=elementgrowth.element_id GROUP BY pets.PET_ID ORDER BY pets.PET_ID"; $result=mysql_query($query); $num=mysql_numrows($result); mysql_close(); echo "Earthpets

"; ?> <?php $i=0; while ($i < $num) { $Peturl=mysql_result($result,$i,"imageurl"); $URLtomap=mysql_result($result,$i,"URLtomap"); $learningslots=mysql_result($result,$i,"learningslots"); $HP=mysql_result($result,$i,"HP"); $Petname=mysql_result($result,$i,"Petname"); $Levelmin=mysql_result($result,$i,"Levelmin"); $Levelmax=mysql_result($result,$i,"Levelmax"); $growth=mysql_result($result,$i,"pets.growth"); $Locations=mysql_result($result,$i,"locidlist"); $Skills=mysql_result($result,$i,"skilllist"); $Element=mysql_result($result,$i,"elementname"); $function=mysql_result($result,$i,"function"); $str=mysql_result($result,$i,"str"); $dex=mysql_result($result,$i,"dex"); $luck=mysql_result($result,$i,"luck"); $con=mysql_result($result,$i,"con"); $charm=mysql_result($result,$i,"charm"); $int=mysql_result($result,$i,"int"); $EL_growth=mysql_result($result,$i,"EL_growth"); ?> <?php $i++; } ?>

[/php]

  Pet name Locations Skills Element
<img src="<?php echo $Peturl; ?>" <?php echo $Petname; ?> <?php echo $Locations; ?> <?php echo $Skills; ?> <?php echo $Element; ?>
Nr. of slots HP Minimum lvl Maximum lvl Str Dex Luck Con Charm Int Growth
<?php echo $learningslots; ?> <?php echo $HP; ?> <?php echo $Levelmin; ?> <?php echo $Levelmax; ?> <?php echo $str; ?> <?php echo $dex; ?> <?php echo $luck; ?> <?php echo $con; ?> <?php echo $charm; ?> <?php echo $int; ?> <?php echo $growth; ?>

First and foremost, stop using mysql_ functions.

Your while loop should be replaced with a for loop.

Other than that, the site looks to be working?

a for loop? ok i will look into that

what do you mean with stop using mysql_functions?

the site works yes. but “Gion Suburbs, Greenville Suburbs, Ilium Suburbs” is now 1 hyperlink to “gion suburbs”.

it should be 3 separte hyperlinks. same for the pet_skills that have multiple results. this is my issue.

Further i do appreciate all imput to improve my coding and make it start and close neatly… i’ve started from scratch, reading about and experimenting with every adding of function. and then put them together. so it may not be entirely perfect this way :stuck_out_tongue:

Well, Michiel, the MySQL functions were “deprecated” a long time ago. They are not allowed in the latest
version of PHP. Once your server is updated to the newest PHP, your code will not work as is. The new and
“improved” version is MySQLi. And, above that is the newer version called PDO. For you, I might suggest
using the MySQLi version as it will require only a few minor changes to your code. You will have to change the
connection to the database in your flconnect.php file and change every place you use the database where you
have a MySQL_ function. These will have to include the DB connection name. Here is a link for how it is used:
http://www.w3schools.com/php/php_mysql_intro.asp
Just press the green Next-Chapter to walk thru the tutorial. Your currently using MySQL “procedural” format.
So, MySQLi Procedural version would match what you currently use. At some point, you should learn PDO as
it is what most programmers on this site am at as the standard.

On your incorrect 3 links, I would start by looking at your database and query to make sure it is pulling the
correct links from your database. The code looks like it is correct, so I am guessing that your data is wrong
or the query is not pulling all of the correct data. To look at the results of your query, you can print the data
using this type of command just after the query. I will display the data that is in the result’s array:
$result=mysql_query($query);
echo “

”;
print_r($result);
die ("

");
This just dumps the array of the results of your query. You can then look toward the end of it and see if it
is pulling out the correct data.

Hope this added info helps…

hmmm i rewrote the connect in mysqli and tried to run a query but I am not sure my webhost even supports this…using www3 school as example:

<?php $con = mysqli_connect("localhost","username","password","dbname"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } ?>

saving this as connect.php

and then trying this sort of…:

<?php include("../connect.php"); $sql="SELECT pets.PET_ID, pets.Petname, FROM pets"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["PET_ID"]. " - Name: " . $row["Petname"]. "
"; } } else { echo "0 results"; } mysqli_close($conn); ?>

it returns : 0 results

pdo is like chinese to me…have to start all over again. by the time (5 years from now) I found enough spare time to get a basic grip there are probably already 5 new programming languages… This is my step up from 1996 html… :stuck_out_tongue:

You used $con= to set up your connection string. Then, you used mysqli_query($conn,$sql) for the query.

As you see, $con is not the same as $conn …

Thanks, sloppy of me. Though after change still 0 results. Anything else here wrong with the coding?

<?php include("../connect2.php"); $sql="SELECT pets.PET_ID, pets.Petname, FROM pets"; $result = mysqli_query($con, $sql); if (mysqli_num_rows($result) > 0) { // output data of each row while($row = mysqli_fetch_assoc($result)) { echo "id: " . $row["PET_ID"]. " - Name: " . $row["Petname"]. "
"; } } else { echo "0 results"; } mysqli_close($con); ?>

print the results of, [php]mysqli_num_rows($result);[/php] and see what number it returns.

Also, your query:

$sql=“SELECT pets.PET_ID, pets.Petname, FROM pets”;

Most likely should just be:

$sql=“SELECT PET_ID, Petname FROM pets”;

No comma before FROM as it thinks you want field FROM to be included also…
( And, make sure those field names and table name is spelled exactly like that. Case is important there! )

Didn’t notice the straggling comma. That would cause a mysql error to b e thrown.

like this? the result is a blank page…

I messed up the connect file on purpose to test, it gave an error. so i assume it IS making a connection to my DB…

http://wmomusic.org/fl1/flpets7.php

[php]<?php
include("…/flconnect2.php");

$sql="SELECT pets.PET_ID, pets.Petname

FROM pets";
$result = mysqli_query($sql);
//$num=mysqli_numrows($result);

//if (mysqli_num_rows($result) > 0) {
// output data of each row
// while($row = mysqli_fetch_assoc($result)) {
// echo "id: " . $row[“PET_ID”]. " - Name: " . $row[“Petname”]. “
”;
// }
//} else {
// echo “0 results”;
//}

mysqli_close();
?>

<?php print mysqli_num_rows($result); ?>[/php]

started new. finally we are making progress:

CONNECT FILE:
[php]<?php
$servername = “localhost”;
$username = “xxxxxx”;
$password = “xxxxxx”;
$dbname=“xxxxx”;

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo “Connected successfully”;
?>

[/php]QUERY FILE:

[php]<?php
include("…/flconnect3.php");

$sql="SELECT PET_ID, Petname, HP

FROM pets";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "PetID " . $row[“PET_ID”]. " - Petname: " . $row[“Petname”]. " " . $row[“HP”]. “
”;
}
} else {
echo “0 results”;
}

$conn->close();
?>
[/php]
It has output :slight_smile: I will continue to test from here. Let’s see what happens to the locations when i make hyperlinks without the rest of the selections

UPDATE http://wmomusic.org/fl1/flpets8.php

The links are working fine. No error in my DB :slight_smile:

so there is some error in the way of thinking somewhere (see coding before).

Somehow i am telling the DB to supply only the link to the FIRST match in the locations list…

by doing:

<?php echo $Locations; ?>

where $Locations is $Locations=mysql_result($result,$i,“locidlist”);

And locidlist is: GROUP_CONCAT(DISTINCT locations.Location ORDER BY locations.Location SEPARATOR ', ') AS locidlist

This will need to be rewritten all of course, but i think that will not solve the problem at hand… hmmmm

What is the effect of group concat as list? Does it see the whole result of the list as 1 variable?

Well, the simplest way to check this is to RIGHT-CLICK on the page where the anchor (Link) is displayed and
select VIEW-SOURCE and look at the link and see how it looks. I bet you can figure it out from there. If it is
not showing the link at all, turn on full error displays and see what is wrong with your PHP code.

further testing confirmed my suspicions:

using no GROUP_CONCAT on the Location selection: http://wmomusic.org/fl1/flpets55.php

Now we get a LOT of rows per “Pet”, but all the links refer to the correct page.

with the GROUP_CONCAT: http://wmomusic.org/fl1/flpets56.php (as before) the whole “concat result” is seen as 1 “item”. It doesn’t know where the next location should start…

Let’s go back to http://wmomusic.org/fl1/flpets8.php

Can i somehow do this FIRST and then make the selection of the rest of the data that i need and then GROUP CONCAT the results of flpets8 ?

So…make the selection in 2 steps?

SOLVED!!!

i can simply do this:

GROUP_CONCAT(DISTINCT ‘’, CONCAT (locations.Location), ‘’ ORDER BY locations.Location SEPARATOR ', ') AS locidlist

I tried something like this before but i used GROUP_CONCAT(DISTINCT <a href= " …

that does not work, this does I was not aware that i’d need to capture it between ’ :slight_smile:

CONGRATS! Always nice to solve a programming puzzle isn’t it!?! Well, we will see you in the next puzzle…

Sponsor our Newsletter | Privacy Policy | Terms of Service