0

I need to know how to sort my authors into their respective categories
I have a mysql table with the authors details and another with the categories this is my code to get the info from the tables
(i realize I spelled category wrong in my database)

$sql = 'select name, providers.id, catid, catagory.catagoryname from providers inner join catagory on catid = catagory.id'; 
$result = mysqli_query($link, $sql);
while($query = mysqli_fetch_array($result))
{
$providers[] = array('name' => $query['name'], 'id' => $query['id'], 'catid' => $query['catid'], 'catname' => $query['catagoryname']);
}

$sql2 = 'select catagoryname from catagory';
$result2 = mysqli_query($link, $sql2);
while($query2 = mysqli_fetch_array($result2))
{
$catnames[] = array('catname' => $query2['catagoryname']);
}

and this is my code to ouput it

<? foreach ($catnames as $catname): ?>
<? echo $catname['catname']; ?>
<? foreach ($providers as $provider): ?>
<div> <? echo $provider['id'] . ' ' . $provider['catid'] . ' ' .  $provider['name']; ?>
 </div>
<? endforeach ?>
<? endforeach ?>

but it just echoes the name of each category and repeats all the information in every single category
I'm new to php and mysql I could use some help
Thanks

3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by jrivera
1

if I understand you want to sort your authors for each category , well U shoul replace line 1 by this :

$sql = 'SELECT name, providers.id, catid, catagory.catagoryname FROM providers INNER JOIN catagory ON catid = catagory.id  ORDER BY providers.id GROUP BY catagory.catagoryname';
0

it would be like this one

$reponse = $bdd->query("SELECT name, providers.id, catid, catagory.catagoryname FROM providers INNER JOIN catagory ON catid = catagory.id  ORDER BY providers.id GROUP BY catagory.catagoryname";
while($donnees = $reponse->fetch( ))
echo "Provoders_id : ".$donnees['providers.id']."category".$donnees['catagory.id']."</br>";

Edited by hicman: n/a

0

It looks like the problem might be in your SQL statement. Do you have a description (schema) of your tables? It would help in writing the SQL statement you're looking for. You may want to check out the following links.

http://en.wikipedia.org/wiki/Join_(SQL)

*Late browser response. I can't delete it!

Edited by jrivera: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.