Echo array based on distinct key - php

I have “ordered by” a database to be in ascending country order, then descending years. Each database record contains: countryname, year, details. There are many duplicate countries, but the years are different. For instance:
Albania, 2000, details
Albania, 1965, details
Croatia, 2014, details
Croatia, 2003, details
Can’t figure out how to echo the array to get results like the following where country is on one line and years & details are listed below without duplicating the name of the country:
Albania
2000, details
1965, details
Croatia
2014, details
2003, details
Seems like I need foreach distinct country, echo year and details?
Here is my php so far:
$result = mysql_query("SELECT country, year, details FROM studies ORDER BY country, year DESC ");
//output data from each row in db
while($row = mysql_fetch_array($result)) {
echo " Country: " .$row['country']. "<br /> Year: " .$row['year']. " Details: ".$row['details']. "<br /><br /> ";
}
Would appreciate any help, I'm stumped!

Try adding a country check:
$newcountry = '';
while($row = mysql_fetch_array($result)) {
if ($newcountry != $row['country']) {
echo "Country:". $row['country']."<br />";
$newcountry = $row['country'];
}
echo " Year: " .$row['year']. " Details: ".$row['details']. "<br /><br /> ";
}
This should work, because you have ordered your query by Country. This is critical, otherwise you should absolutely add a GROUP BY clause to your SQL.
EDIT: to add a <div> around the group, you simply would change the echo sequence, checking first to see if the country has already been set once. It would look like:
$newcountry = 'undefined';
while($row = mysql_fetch_array($result)) {
if ($newcountry !='undefined' && $newcountry != $row['country']){
echo '</div>'; // only add a closing div if a new country (and not 1st)
}
if ($newcountry != $row['country']) {
echo "Country:". $row['country']."<br /><div class='countryDetail'>";
$newcountry = $row['country'];
}// added the start of the <div>
echo " Year: " .$row['year']. " Details: ".$row['details']. "<br /><br /> ";
}
if ($newcountry != 'undefined') { //make sure at least one <div> is set
echo "</div>"; // close the last <div>
}
I added the class countryDetail to the div, so you can use this with toggle in your jQuery.

You can use nested while loops. You might also want to use PDO/mysqli_ functions/prepared statements in place of mysql_ functions:
// get unique country list first
$sql1 = "SELECT DISTINCT(country) FROM studies ORDER BY country";
$result1 = mysql_query($sql1);
// iterate through result set of sql1
while($row1 = mysql_fetch_array($result1))
{
$country = $row1['country'];
echo "<br>"; // new line
echo $country;
// get year, details for each country
$sql2 = "SELECT year, details FROM studies WHERE country = '$country' ORDER BY year DESC";
$result2 = mysql_query($sql2);
// iterate through result set of $sql2
while ($row2 = mysql_fetch_array($result2))
{
echo "<br>"; // new line
echo $row2['year']. ", " . $row2['details'];
}
}

loop party!
$rows = [];
while($row = mysql_fetch_array($result)) {
$rows[ $row['country'] ] = [ $row['year'], $row['details'] ];
}
foreach($rows as $country => $row) {
$details = array_map(function($items) { return sprintf("\t - %s: %s\n", $items[0], $items[1]) }, $row);
echo sprintf("\n%s:\n %s", $country, $details);
}

Related

Why is mysqli_fetch_row preventing all my results from displaying?

I'm making a survey tracking website and I'm having trouble. I want to display all surveys that have been completed in the last 7 days. I'm using mysqli_fetch_row to see if any rows are retrieved, and if they are display them. If they aren't any compelted in the last 7 days, I want it to display the words "no recently compelted surveys to show."
<?php
require('db/connect.php');
if (!isset($_GET['sort'])) {
$sort = 'client_id';
} else {
$sort = $_GET['sort'];
}
if ($result = $db->query("SELECT client_id, date_added, client, email, date_sent, date_completed FROM clients NATURAL JOIN surveys WHERE date_completed BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() ORDER BY $sort")) {//shows surveys completed in the last 7 days
if (mysqli_fetch_row($result) == 0) {
echo "No recently completed surveys to show.";
} else {
echo "<table>";
echo "<tr><th><a href='portal.php?sort=client_id'>ID</a></th><th><a href='portal.php?sort=date_added'>Date Added</a></th><th><a href='portal.php?sort=client'>Client</a></th><th><a href='portal.php?sort=email'>Email</a></th><th><a href='portal.php?sort=date_sent'>Sent</a></th><th><a href='portal.php?sort=date_completed'>Completed</a></th>";
$rows = $result->num_rows;
for ($num = 0; $num < $rows; ++$num) {
$row = $result->fetch_array(MYSQLI_NUM);
$client_id = $row[0];
$date = $row[1];
$client = $row[2];
$email = $row[3];
$sent = $row[4];
$completed = $row[5];
echo "<tr>";
echo "<td>$client_id</td>";
echo "<td>$date</td>";
echo "<td>$client</td>";
echo "<td>$email</td>";
echo "<td>$sent</td>";
echo "<td><a href='survey/completed/index.php?id=$client_id'>$completed</a></td>";
echo "</tr>";
}
echo "</table>";
}
}
?>
When I remove the if clause for mysqli_fetch_row, it displays ALL of the recently completed surveys, but if I leave it in, it ALWAYS leaves one out. Can anyone help?
You are discarding the first when you do the test. You can get the number of rows like this instead:
if ($result->num_rows == 0) {
echo "No recently completed surveys to show.";
} else {
// ....
}
Docs : mysqli_result::$num_rows

Array duplicating information

My apologies if this has been addressed - I have been searching all day and haven't found anything that meets my needs.
I have a foreach loop in php that is repeating information ad nauseum, and I am not sure how to fix it. I have tried grouping, array_unique, etc., and haven't found a solution. No row is a true duplicate of another when all variables are taken into account.
I have a table where each line represents a winning ticket. Each line has a unique ID, a date, a tier (1,2,3), and some other variables worth of information. I would like to organize this by date, ascending by tier number, where the date is listed once and each ticket is listed once. Right now, each row is listed as a ticket, but repeats multiple times. Here is my code - I have removed the displays in between as those work fine:
$selectResults = "SELECT * FROM mytable WHERE YEAR(date) = 2014 ORDER BY date DESC, tier ASC";
$getResults = #mysqli_query($connect, $selectResults) or die('query error: ' . mysqli_error($connect));
if(mysqli_num_rows($getResults) == 0){
echo "There are no tickets to display.";
}else{
echo "<table><tr><th>Date</th><th>Tier</th><th>Points</th><th>Prize Amount</th></tr>";
while($row = mysqli_fetch_array($getResults)){
extract($row);
foreach($row as $ticket => $date){
echo "<tr><td>" . date('n/j/Y', strtotime($date)) . "</td><td> </td><td> </td><td> </td></tr>";
if (1 == $tier)
{
(Display Tier 1 tickets for date)
}
if (2 == $tier)
{
(Display Tier 2 tickets for date)
}
if (3 == $tier)
{
(Display Tier 3 tickets for date)
}
}
echo "<tr class='bottomRow'><td colspan='4' /></tr>";
}
echo "</table>";
Get rid of the foreach loop:
foreach ($row as $ticket => $date)
You're already looping over the rows with the while loop. $row just contains a single row, with each column as an element of the array.
Your code should look like:
$last_date = null;
while ($row = mysqli_fetch_assoc($getResults)) {
extract($row);
if ($date != $last_date {
echo "<tr><td>" . date('n/j/Y', strtotime($date)) . "</td><td> </td><td> </td><td> </td></tr>";
$last_date = $date;
}
// Display ticket details
}

Assign PHP variable to each row

The following mysql query is getting the last 4 records from the 'residential' table but I'm trying to assign a php variable ($postcode) to each row (the 3rd [3] column in particular) for use in on another page. The following doesn't seem to be split the rows out correctly to assign to each variable?
<?php
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM residential ORDER BY My_id DESC LIMIT 4");
$postcodes = array();
while ($row = mysqli_fetch_array($result))
{
$postcodes[] = $row[3];
}
echo "location one " . $postcodes[0];
echo "<br>";
echo "location two " . $postcodes[1];
echo "<br>";
echo "location three " . $postcodes[2];
mysqli_close($con);
?>
fetch calls fetch a single ROW of data. You're just assigning column #3 of each row, over and over again.
You probably want:
$postcodes = array();
while ($row = ...) {
$postcodes[] = $row[3];
}
var_dump($postcodes);

Efficient Method To Display Group Title Once — PHP/MySQL/jQuery

There are three tables in MySQL: Employees, Branches, Departments. I need information to appear in the following way:
ATLANTA Branch Delivery Department Phillip J. Fry Phone: 123456
Engineering Department Turanga Leela Phone: 123457
Bender Rodriguez Phone: 123458
The simple PHP code currently:
1) Takes rows from three tables (simple SELECT query with JOIN)
2) Puts them in row (mysql_fetch_assoc)
3) Displays using the PHP While loop
The result is then like this:
ATLANTA Branch Delivery Department Phillip J. Fry Phone: 123456
ATLANTA Branch Engineering Department Turanga Leela Phone: 123457
ATLANTA Branch Engineering Department Bender Rodriguez Phone: 123458
What technique (JS, jQuery, Ajax) or method can you recommend so I can pull row information using only one query and not duplicate the Branch name and Department name?
UPDATE: If I put the branch name outside the loop (using While loop), there would be multiple loops: 1) To get a branch, 2) To get a department, 3) To get all employees in that department. Loop.
UPDATE: Sharing the code:
<?php
// Create connection
$connection = mysql_connect('localhost','root', '') or die('Connection error.');
mysql_query("SET NAMES 'utf8'", $connection);
mysql_select_db("eReference");
// Check Employees
$query = "SELECT Employees.fName, Employees.lName, Department.deptName, Branch.branchName, ".
"FROM Employees ".
"LEFT JOIN Department ".
"ON Employees.department = Department.id ".
"LEFT JOIN Branch ".
"ON Employees.branch = Branch.id ;";
$result = mysql_query($query, $connection) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
?>
<h2><?php echo $row['branchName']; ?></h2>
<?php if ($row['deptName']) echo "<h3>" . $row['deptName'] . "</h3>"; ?>
<h4><?php echo $row['fName'] . " " . $row['lName']; ?></h4></p>
<?php
}
?>
<?php
$i = 1; // to be incremented after printing branchName once
while ($row = mysql_fetch_assoc($result)) {
if($i == 1) { ?>
<h2><?php echo $row['branchName']; $i ++; ?></h2>
<?php } ?>
<?php if ($row['deptName']) echo "<h3>" . $row['deptName'] . "</h3>"; ?>
<h4><?php echo $row['fName'] . " " . $row['lName']; ?></h4></p>
<?php } ?>
Just add a variable $i = 1 and check before printing if it is equal to 1. After printing it for first time, increment it.
It is just addition of an if statement.
Hope this helps.
This is how I would do it.
Create a multi-dimensional array with the data, and iterate through the array to render the output.
This will not be the most efficient in terms of memory usage, but unless you have thousands of rows of data, it probably won't be an issue.
The benefit of this, is that the html rendering code is much simpler and easier to understand, plus sql and html are not intermingled. (which is good for code maintenance)
<?php
// Create connection
$connection = mysql_connect('localhost','root', '') or die('Connection error.');
mysql_query("SET NAMES 'utf8'", $connection);
mysql_select_db("eReference");
// Check Employees
$query = "SELECT Employees.fName, Employees.lName, Department.deptName, Branch.branchName, ".
"FROM Employees ".
"LEFT JOIN Department ".
"ON Employees.department = Department.id ".
"LEFT JOIN Branch ".
"ON Employees.branch = Branch.id ;";
// note you probably want to add an order by statement here too, to ensure consistent sorting
$result = mysql_query($query, $connection) or die(mysql_error());
$data = array();
// build a multi-dimensional array from the result set
while ($row = mysql_fetch_assoc($result)) {
$data[($row['branchName'])][($row['deptName'])][] = array(
'name' => "{$row['fName']} {$row['lName']}",
'phone' => $row['phone'] // add phone, doesn't exist in original query, but just to illustrate how it would work
);
}
// sql finishes here
?>
<?php
// html rendering
// use htmlentities to escape any html chars, such as < > etc
foreach ($data as $branchName => $departments) {
echo '<h2>',htmlentities($branchName),'</h2>';
foreach ($departments as $deptName => $employees) {
foreach ($employees as $employee) {
echo '<h3>',htmlentities($deptName),'</h3>';
echo '<h4>',htmlentities($employee['name']),'</h4>';
echo '<h4>',htmlentities($employee['phone']),'</h4>';
}
}
}
?>
The result gotten from your sql should be an array so use a while loop to iterate throw the array while echo the result of the current index
Maybe this will work..
<?php
$deptName;
while($row = mysql_fetch_assoc($result))
{
if ($row['deptName'])
{
if ($deptName != $row['deptName'])
{
echo "<h3>" . $row['deptName'] . "</h3>";
$deptName = $row['deptName'];
}
}
}
?>
$result = mysql_query($query, $connection) or die(mysql_error());
$newarray = array();
$i = 0;
while ($row = mysql_fetch_assoc($result)) {
$newarray[$i]['deptName'] = $row['deptName'];
$newarray[$i]['fName'] = $row['fName'];
$newarray[$i]['lName'] = $row['lName'];
$i++;
}
<h2><?php echo $newarray[0]['branchName']; ?></h2>
while($newarray){
?>
<?php if ($newarray['deptName']) echo "<h3>" . $newarray['deptName'] . "</h3>"; ?>
<h4><?php echo $newarray['fName'] . " " . $newarray['lName']; ?></h4></p>
}
?>
So, this is what it did:
$query = "SELECT Employees.lName, Employees.fName, Employees.mName, Position.position, ".
"department.deptName, department.deptId, ".
"Branch.branchName, Branch.branchId, ContactInformation.* ".
"FROM Employees ".
"LEFT JOIN Position ".
"ON Employees.position = Position.id ".
"LEFT JOIN Department ".
"ON Employees.department = Department.deptId ".
"LEFT JOIN Branch ".
"ON Employees.branch = Branch.branchId ".
"LEFT JOIN ContactInformation ".
"ON Employees.contactInformation = ContactInformation.id ".
"ORDER BY Employees.branch, Employees.department ASC;";
$result = mysql_query($query, $connection) or die(mysql_error());
$arrayOfEmployees = array();
while ($row = mysql_fetch_assoc($result)) {
$arrayOfEmployees[($row['branchName'])][($row['deptName'])][] = array(
'lName' => $row['lName'],
'fName' => $row['fName'],
'mName' => $row['mName'],
'position' => $row['position'],
'lPhone1' => $row['lPhone1'],
'lPhone2' => $row['lPhone2'],
'mPhone' => $row['mPhone'],
'fax' => $row['fax'],
'office' => $row['office'],
'email' => $row['email']
);
}
foreach($arrayOfEmployees as $branchName => $arrayOfDepartments) {
echo "<h2>".$branchName."</h2>";
foreach($arrayOfDepartments as $deptName => $arrayOfEmployeeContacts) {
echo '<h3>',htmlentities($deptName),'</h3>';
foreach($arrayOfEmployeeContacts as $employeeContacts) {
echo "<h4>".$employeeContacts["lName"]." ".$employeeContacts["fName"]." ".$employeeContacts["mName"]."</h4>";
echo "<p>";
if($employeeContacts["position"]) echo $employeeContacts["position"]."<br>";
$num = $employeeContacts["lPhone1"];
if($employeeContacts["lPhone1"]) echo "+".substr($num,0,1)." (".substr($num,1,4).") "." ".substr($num,5,2)."-".substr($num,7,2)."-".substr($num,9,2)."<br>";
$num = $employeeContacts["lPhone2"];
if($employeeContacts["lPhone2"]) echo "+".substr($num,0,1)." (".substr($num,1,4).") "." ".substr($num,5,2)."-".substr($num,7,2)."-".substr($num,9,2)."<br>";
$num = $employeeContacts["mPhone"];
if($employeeContacts["mPhone"]) echo "+".substr($num,0,1)." (".substr($num,1,3).") "." ".substr($num,4,3)."-".substr($num,7,2)."-".substr($num,9,2)."<br>";
$num = $employeeContacts["fax"];
if($employeeContacts["fax"]) echo "+".substr($num,0,1)." (".substr($num,1,4).") "." ".substr($num,5,2)."-".substr($num,7,2)."-".substr($num,9,2)."<br>";
if($employeeContacts["email"]) echo "".$employeeContacts["email"]."<br>";
if($employeeContacts["office"]) echo "Кабинет — ".$employeeContacts["office"];
echo "</p>";
}
}
}
Tested. The solution works well dynamically — just add more branches and departments into the database. Here is the efficiency check against the method I originally used (in microseconds):
Array-based Original
1 1.015 1.012
2 1.016 1.02
3 1.026 1.013
4 1.015 1.002
5 1.026 1.02
6 1.014 1.02
7 1.013 1.019
8 1.005 1.014
9 1.013 1.006
10 1.021 1.015
Average 1.0164 1.0141

php/mysql: Append 2 or more foreign keys to one 'query'?

My solution to this is at the bottom
My issue is: I am trying to display foreign key data, but because there is more than one foreign key, I am getting a 'duplicate' query for each of the foreign keys.
http://i.imgur.com/Gfqx497.png
As you can see, I can query the correct data, but I don't know how to attach the other foreign key data to the same 'one line output'.
I've been lurking stackoverflow for a while to find an answer to my problem and I'm at a wits end. I have found quite a number of threads, such as the two links below, where I believe people are asking the same thing, however I can't seem to wrap my head around getting the solution to work in my case. From my understanding, I need to be using aliases for the tables, however I've tried multiple different interpretations of the solutions and can't recreate the solution.
How do I merge two or more rows based on their foreign key
mysql query 2 foreign keys
--
I've got two tables ('Minions and Ability'), one of which has four foreign keys linking to the other.
http://i.imgur.com/ctpFHur.png
This is the php code that I'm using for the query, which is mostly taken from PHP and MySQL Web Development 4th Edition (Welling, Thomson) which I purchased to get me started with php and mysql.
$query = "SELECT minions.name, minions.summon, minions.attack,
minions.health, minions.race, minions.rarity,
minions.ability1, minions.ability2, minions.ability3,
minions.ability4, minions.imagebig,
ability.ability
AS ability FROM minions
INNER JOIN ability on
minions.ability1 = ability.abilityid
OR minions.ability2=ability.abilityid";
//Only trying for 2 foreign keys to try get it to work
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
//echo "<p><strong>".($i+1).". Name: ";
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['summon']);
echo "<br />Attack: ";
echo stripslashes($row['attack']);
echo "<br />Health: ";
echo stripslashes($row['health']);
echo "<br />Race: ";
echo stripslashes($row['race']);
echo "<br />Rarity: ";
echo stripslashes($row['rarity']);
//if (stripslashes($row['ability'] != NULL)){
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
//}
echo "<br />";
$imageMinion = stripslashes($row['imagebig']);
// $iwidth = 25;
// $iheight = 100;
// echo '<img src="img/'.$imageMinion.'.png" style="width:'.$iwidth.'px;height:'.$iheight.'px;">';
//echo "<br />";
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
Could someone please guide me to getting this to display correctly? I've tried to follow the other solutions and just can't seem to get the alias naming correct, if I'm correct in thinking that is the solution.
========EDIT REGARDING ANSWER FROM verbumSapienti===========
I am embarrassingly unable to get your Answer to work. This is how the code looks.
$query = "SELECT minions.name, minions.summon, minions.attack, minions.health,
minions.race, minions.rarity, minions.ability1, minions.ability2,
minions.ability3, minions.ability4, minions.imagebig,
ability.ability
AS ability
FROM minions
INNER JOIN ability
ON minions.ability1 = ability.abilityid
OR minions.ability2 = ability.abilityid
OR minions.ability3 = ability.abilityid
OR minions.ability4 = ability.abilityid";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc();
$abilitiesArr[] = $row2[$ability];
}
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['summon']);
echo "<br />Attack: ";
echo stripslashes($row['attack']);
echo "<br />Health: ";
echo stripslashes($row['health']);
echo "<br />Race: ";
echo stripslashes($row['race']);
echo "<br />Rarity: ";
echo stripslashes($row['rarity']);
foreach($abilitiesArr as $ability)
{
$q = "SELECT $ability FROM ability";
$result = $db->query($q);
$row = $result->fetch_assoc();
echo "<br />Ability: $row";
}
/*if (stripslashes($row['ability'] != NULL)){
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
}*/
echo "<br />";
$imageMinion = stripslashes($row['imagebig']);
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
}
I've tried changing around a few things and haven't had any success. As is, I get the following error:
Fatal error: Call to a member function fetch_assoc() on a non-object in D:\Xampp\htdocs\ocduels\results.php on line 87
Which is:
$row2 = $result->fetch_assoc();
In:
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc();
$abilitiesArr[] = $row2[$ability];
}
::MY SOLUTION TO THIS::
This seems to work. I don't think its efficient, but its enough to allow me to continue learning. Thank you for all the responses. This allows me to find a 'Minion' and only have one instance of the 'Minion' when there is more than 1 Foreign Key with data.
$query = "SELECT
m.name as m_name,
m.summon as m_summon,
m.attack as m_attack,
m.health as m_health,
m.race as m_race,
m.rarity as m_rarity,
m.ability1 as m_ability1,
m.ability2 as m_ability2,
aa.ability as a_ability,
ab.ability as b_ability,
m.imagebig as m_imagebig
FROM minions m
LEFT JOIN ability aa
ON m.ability1 = aa.abilityid
LEFT JOIN ability ab
ON m.ability2 = ab.abilityid";
$result = $db->query($query);
$num_results = $result->num_rows;
echo "<p>Number of items found: ".$num_results."</p>";
for ($i=0; $i <$num_results; $i++){
$row = $result->fetch_assoc();
echo "<p><strong>";
echo htmlspecialchars(stripslashes($row['m_name']));
echo "</strong><br />Summoning cost: ";
echo stripslashes($row['m_summon']);
echo "<br />Attack: ";
echo stripslashes($row['m_attack']);
echo "<br />Health: ";
echo stripslashes($row['m_health']);
echo "<br />Race: ";
echo stripslashes($row['m_race']);
echo "<br />Rarity: ";
echo stripslashes($row['m_rarity']);
if (stripslashes($row['a_ability'] != NULL)){
echo "<br />Ability 1: ";
echo stripslashes($row['a_ability']);
}
if (stripslashes($row['b_ability'] != NULL)){
echo "<br />Ability 2: ";
echo stripslashes($row['b_ability']);
}
echo "<br />";
$imageMinion = stripslashes($row['m_imagebig']);
echo '<img src="img/'.$imageMinion.'.png">';
echo "</p>";
}
Try DISTINCT keyword to restrict duplicate values.
SELECT DISTINCT minions.name, minions.summon, minions.attack,
minions.health, minions.race, minions.rarity,
minions.ability1, minions.ability2, minions.ability3,
minions.ability4, minions.imagebig,
ability.ability
AS ability FROM minions
INNER JOIN ability on
minions.ability1 = ability.abilityid
OR minions.ability2=ability.abilityid";
you could try a subquery that prints only the ability text for each ability ID contained in each minion's attributes, maybe something along the lines of:
$abilities = array('ability1', 'ability2', 'ability3', 'ability4');
foreach($abilities as $ability)
{
$q = "SELECT $ability FROM minions WHERE name={$row['name']}";
$result = $db->query($q);
$row2 = $result->fetch_assoc()
$abilitiesArr[] = $row2[$ability];
}
then replace
echo "<br />Abilty: ";
echo stripslashes($row['ability']);
with
foreach($abilitiesArr as $ability)
{
$q = "SELECT $ability FROM ability";
$result = $db->query($q);
$row = $result->fetch_assoc()
echo "<br />Ability: $row";
}

Categories