Jump to content

Join Problem


Go to solution Solved by Barand,

Recommended Posts

The problem that I am having is that it will only display 1 output for each farea.  Not sure what the problem is

 

$queryfa = "SELECT  * FROM faq as f1 join faqcat as f2 where f1.farea = f2.cat_id group by f1.farea order by f1.ftitle DESC";

 

Database layout is:

Table: 
faq

Rows:
farea
ftitle
fdesc
posted

Table:
faqcat

Rows:
cat_id
cat_name


 

 

Link to comment
https://forums.phpfreaks.com/topic/275933-join-problem/
Share on other sites

The query that I posted will only give 1 output per farea,  There is more than 1 entry per farea

 

So what is displaying is:

 

cat_name1

ftitle1  fdesc1 posted1

 

cat_name2

ftitle2 fdesc2 posted2

 

cat_name3

ftitle5 fdesc5 posted5

 

 

but there is 

cat_name1

ftitle1 des...

ftitle3 des...

 

cat_name2

ftitle2 .....

ftitle4....

 

cat_name3

ftitle5

etc....

Edited by SkyRanger
Link to comment
https://forums.phpfreaks.com/topic/275933-join-problem/#findComment-1419890
Share on other sites

Tried to edit post.  But here is my full code that I am working with:

 

 <?php
						$queryfa = "SELECT  * FROM faq as f1 join faqcat as f2 where f1.farea = f2.cat_id group by f1.farea order by f1.ftitle DESC";
                        $resultfa = $mysqli->query($queryfa) or die($mysqli->error.__LINE__);
						

                        while($rowfa = $resultfa->fetch_assoc()){
							
						?>
				<div class="grid_16">
					<table>
						<thead>
							<tr>
								<th colspan="3">FAQ - <?php echo $rowfa['cat_name']; ?></th>
								<th colspan="2">Actions</th>
							</tr>
						</thead>
						<tfoot>
							<tr>
								<td colspan="5" class="pagination">
									View More
								</td>
							</tr>
						</tfoot>
						<tbody>
                           <tr>
                               <td width="21%"><u><em><strong>Title</strong></em></u></td>
                               <td width="50%"><u><em><strong>Description</strong></em></u></td>
                               <td width="17%"><u><em><strong>Date Added</strong></em></u></td>
                               <td width="6%"></td><td width="6%"></td>
                           </tr>    
							<tr>
								<td><?php echo $rowfa['ftitle']; ?></td>
								<td><?php echo $rowfa['fdesc']; ?></td>
								<td><?php echo $rowfa['posted']; ?></td>
								<td><a href="viewfaq.php?faq=<?php echo $rows['fid']; ?>" class="edit">View</a></td>
								<td><a href="delfaq.php?faq=<?php echo $rows['fid']; ?>" class="delete">Delete</a></td>
							</tr>
						<?php
						}
                        ?>	
						</tbody>
					</table>
				</div>
			</div>
Edited by SkyRanger
Link to comment
https://forums.phpfreaks.com/topic/275933-join-problem/#findComment-1419904
Share on other sites

  • Solution

here's how

 

$sql = "SELECT f2.cat_name, f1.ftitle, f1.fdesc, f1.posted
        FROM faq as f1
            INNER JOIN faqcat as f2 ON f1.farea = f2.cat_id
        ORDER BY f1.area, f1.title DESC";
$result = mysqli->query($sql);

$prevcat = '';
while (list($catname, $title, $desc, $posted) = $result->fetch_row()) {
    // if it's a new catname, print it
    if ($prevcat != $catname) {
        echo "<br><b>$catname</b><br>";
        $prevcat = $catname;
    }
    echo "$title $desc $posted<br>";
}
Link to comment
https://forums.phpfreaks.com/topic/275933-join-problem/#findComment-1419906
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.