Only show if exists in other table - php

I'm new to PHP and MySQL and right now I'm showing a table with categories from products. But I'm trying to only show them if they exist in another table. Unfortunately, I'm not sure how to get there.
Currently, I'm showing ALL categories like this:
<?php include '/../connect.php'; ?>
<?php
$results=$conn->query("SELECT * FROM groepen");
while($rows=$results->fetch_array())
{
?>
<li><?php echo $rows['GroepOmschrijving']; ?>
<?php
$result=$conn->query("SELECT * FROM subgroepen WHERE GroepID=".$rows['GroepID']);
?>
<ul>
<?php
while($row=$result->fetch_array())
{
?><li><?php echo $row['SubGroepOmschrijving']; ?></li><?php
}
?>
</ul>
</li>
<?php
}
?>
So, I'm trying to only show the categories if the value of table groepen.GroepID matches the value of occasions.Groepnummer.

Related

Issue in pulling pages from the database

I am having problems pulling the pages through in PHP and HTML I have used :-
<li>
<!-- Pulling Categories from the database
dynamically -->
<?php
$nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject =
mysqli_fetch_assoc($nav_subjects)) {
?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
Which pulls the categories dynamically from the database and displays them with a drop down arrow just how I wanted but the pages will not show underneath them here's the code I have used for that bit:-
<!-- Categories listed correctly let's pull the pages for each one -->
<?php
if($nav_subject['id'] == $subject_id) {
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) {
?>
<ul>
<li>
<?php echo h($nav_page['menu_name']); ?>
</li>
</ul>
<?php } // while $nav_pages
} // if($nav_subject['id'] == $subject_id)
} // while $nav_subjects ?>
</li>
<?php
mysqli_free_result($nav_subjects);
mysqli_free_result($nav_pages);
?>
I am pulling in the SQL from another page which is loaded correctly as the categories load and display correctly.
I will be grateful for any ideas.
I have also tried to echo back the sql result but nothing is shown.
I have now got it working with the following code:-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);
while($nav_subject = mysqli_fetch_assoc($nav_subjects)) {?>
<span class="opener"><?php echo h($nav_subject['menu_name']); ?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<li><?php echo h($nav_page['menu_name']); ?></li>
<?php } ?>
<?php } ?>
</ul>
But now it is listing the secondary subjects as a child of the first instead of individual subjects of their own.
*********Resolved**********
please advise if you think the code could be better i've currently used :-
<li>
<?php $nav_subjects = find_all_subjects(['visible' => $visible]);?>
<?php while($nav_subject = mysqli_fetch_assoc($nav_subjects)){?>
<span class="opener"><?php echo h($nav_subject['menu_name']);?></span>
<ul>
<?php if($nav_subject['id'] == $subject_id);
$nav_pages = find_pages_by_subject_id($nav_subject['id'], ['visible' => $visible]);
while($nav_page = mysqli_fetch_assoc($nav_pages)) { ?>
<a href="<?php echo url_for('index.php?id=' . h(u($nav_page['id']))); ?>">
<?php echo h($nav_page['menu_name']); ?></a>
<?php } ?>
<?php mysqli_free_result($nav_pages); ?>
</ul>
<?php } ?>
<?php mysqli_free_result($nav_subjects); ?>
</li>

Mysqli query not working properly in if/else statement

I have a table in MySQL and I want to check in PHP whether there are any 'tasks' written (it's a todo list webapp). If there aren't I want to display a message 'You haven't added any tasks yet', problem is it's not displaying.
PHP is assuming that the list of tasks is never empty, when for user 1 (in PHP current user_id = 2), for instance, it is, since I haven't inserted any tasks in my MySQL for User 1 (all tasks are user 2).
<?php
global $connection;
$user = $_SESSION['user_id'];
$query = "SELECT * FROM to_do_list WHERE user = $user";
$items = mysqli_query($connection, $query);
?>
<div>
<?php if(!empty($items)): ?>
<ul id="myUL">
<?php foreach($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"><?php echo $item['task']; ?></span>
<?php if(!$item['done']): ?>
Done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p>You haven't added any tasks yet.</p>
<?php endif; ?>
</div>
mysqli_query returns a "resource" not an array - so:
instead of foreach($items as $item): use while($item = mysqli_fetch_assoc($items))
and now every $item is an associated array of results (meaning - you can call the output using it's name like $item['done'])
also - instead of if(!empty($items)) you should use if(mysqli_num_rows($items) > 0)

If column value = NO don't display

I have an if statement
<p>Included Services</p>
<?php foreach($services as $service): ?>
<?php if(!empty($service['service_webdesign'])){ ?>
<div><strong>web Design:</strong> <?php echo $service['service_webdesign'] ?></div>
<?php } ?>
<?php endforeach ?>
<p>Services charged by the hour</p>
<?php foreach($services as $service): ?>
<?php if(!empty($service['service_webdesign'])){ ?>
<div><strong>Service:</strong> <?php echo $service['service_webdesign'] ?></div>
<?php } ?>
<?php endforeach ?>
There are three possible values Yes, hourly rate or no.
In the first block I want to be able to echo the service column values
<div><strong>Service:</strong> <?php echo $service['service_webdesign'] ?></div>
only if the database value is 'yes'.
In the second block I want to echo the service fee column
<div><strong>Web Design:</strong> <?php echo $service['service_fee_webdesign'] ?>
</div>
only if the value of the 'service_webdesign' is set to 'hourly rate' but I do not actually want to show the value of 'service_webdesign' ('hourly rate').
An example of my database table structure is as follows
service_webdesign = Yes
service_fee_webdesign = NULL
service_graphicdesign = Hourly
service_fee_graphicdesign = 20
How can I achieve that within an if statement? Or would I need to create another SQL query for each of the blocks?

Loop with a Loop including an explode()

I have an associative array returning a SQL query that pulls questions from my database. The tags field is stored as a delimited text VarChar. I want to loop through all of my database entries while using explode to create an array and then loop through the tag array to create a UL with the tags as well unfortunately it has not worked very well. This is what I have so far:
<?php foreach($questionRow as $questionShow) { ?>
<?php echo ($questionShow['netvotes']) ;?>
<?php echo ($questionShow['views']) ;?>
<?php echo ($questionShow['q_answer_count']) ;?>
<?php echo ($questionShow['title']) ;?>
Tags:
<ul style="display: inline">
<?php
$tagname = explode(",",$questionShow['tags']);
foreach ($tagname as $tagList) { ?>
<li class="label label-inverse" style="margin-left: 5px"><?php echo($tagList) ;?></li>
<?php }; ?>
</ul>
<?php }; ?>
The results were very strange
I also tried by using the below to run my outer loop
<?php while($questionShow =$questionResult>fetch_assoc() ) { ?>
That caused the main recordset results to not actually loop. Hopefully I have explained this properly and someone could help. Thank you in advance!
well this could be very simple:
<?php
$query = mysql_query("SELECT * FROM `questions` LIMIT 30"); // replace this with your query
while($data=mysql_fetch_array($query)){
echo $data['netvotes'];
echo $data['views'];
echo $data['q_answer_count'];
echo $data['title']) ;?>
?>
Tags:
<ul style="display: inline">
<?php
foreach(explode(",",$data['tags']) as $tag){
?>
<li class="label label-inverse" style="margin-left: 5px"><?php echo $tag; ?></li>
<?php
}
?>
</ul>
<?php
}
?>

Help with a foreach loop

In my website I am trying to display all the applicants to jobs that a user has posted, basically I want the out put to simimlar too,
Job Title 1
Aplicant Name 1
Aplicant Name 2
Applicant Name 3
Job Title 2
Applicant Name 4
Application Name 5
Basically I want the applications to be gathered under the jobs they applied for, however the out put I am current getting is,
Job Title 1
Application Name 1
Job Title 1
Applicant Name 2
The code I am using to do this foreach loop is as follows
<?php foreach($applications as $a) : ?>
<h3><?php echo $a['jobtitle']; ?></h3>
<li>
<img src="/media/uploads/candidates/<?php echo preg_replace('/(.gif|.jpg|.png)/', '_thumb$1', $a['profile_image']);?>" width="90" height="60"/>
<p><?php echo $a['name']; ?></p>
</li>
<?php endforeach; ?>
We need the data structure to properly answer, but I'm assuming you need a nested foreach...
<? foreach($jobs as $j): ?>
# list jobs
<? foreach($applicants as $a): ?>
<? if ($j['jobtitle'] == $a['jobtitle']): ?>
# list applicants
<? endif; ?>
<? endforeach; ?>
<? endforeach; ?>
Well you are getting that because you are only outputting
$a['jobtitle']
and
$a['name']
Where are the other names stored? Something like this is probably what you want, although that won't work if you copy/paste it as it seems that $applications[x]['name'] is not an array:
<?php foreach($applications as $a) : ?>
<h3><?php echo $a['jobtitle']; ?></h3>
<li>
<img src="/media/uploads/candidates/<?php echo preg_replace('/(.gif|.jpg|.png)/', '_thumb$1', $a['profile_image']);?>" width="90" height="60"/>
<?php foreach($a['name'] as $name)?>
<p><?php echo $name; ?></p>
<?php endforeach; ?>
</li>
<?php endforeach; ?>
I recommend pulling jobtitle and applicants from the database and storing them in a multidimensional array.
$jobs[$jobtitle][] = $applicant_name;
Then looping through this array in your view. I've omitted your image code for clarity.
foreach($jobs as $title=>applicants){
echo $title;
foreach($applicants as $name){
echo $name;
}
}

Categories