If field == null display else don't - php

My recordset is:
mysqli_select_db($KCC, $database_KCC);
$query_rsOtherServices = "SELECT pagecontent.mainMenuID, mainmenu.mainMenuLabel, pagecontent.subMenuID, submenu.subMenuLabel, pagecontent.contentID, pagecontent.contentTitle FROM submenu RIGHT JOIN (mainmenu RIGHT JOIN pagecontent ON mainmenu.mainMenuID = pagecontent.mainMenuID) ON submenu.subMenuID = pagecontent.subMenuID WHERE pagecontent.mainMenuID = 1 AND pagecontent.subMenuID IS NULL";
$rsOtherServices = mysqli_query($KCC, $query_rsOtherServices) or die(mysqli_error());
$row_rsOtherServices = mysqli_fetch_assoc($rsOtherServices);
$totalRows_rsOtherServices = mysqli_num_rows($rsOtherServices);
and the code I'm using to display the record is:
<?php do { ?>
<li><h4><?php echo $row_rsOtherServices['contentTitle']; ?></h4></li>
<?php } while ($row_rsOtherServices = mysqli_fetch_assoc($rsOtherServices)); ?>
This all works fine if a record exists but if there is no record, a 'link' is available to click even though it's not visible.
I have tried <?php if ($totalRows_rsOtherServices['subMenuID'] === Null) { ?>, <?php if ($totalRows_rsOtherServices['subMenuID'] > 0) { ?>, <?php if ($totalRows_rsOtherServices['subMenuID'] == true) { ?>, <?php if ($totalRows_rsOtherServices['subMenuID'] == false) { ?>but to no avail.
I know nothing about programming and even less about PHP so I'm not even sure is I'm going in the right direction.
I need to get rid of the 'invisible link'.

Because you are using do ... while, use while only.
Try
<?php while (($row = mysqli_fetch_assoc($rsOtherServices))) { ?>
<li>
<a href="familyservices.php?idVal=<?php echo $row['contentID']; ?>">
<h4><?php echo $row['contentTitle']; ?></h4>
</a>
</li>
<? } ?>

OK, I've figured it out.
Instead of using $((totalRows_rsOtherServices['subMenuID']) > 0), I changed it to (($row_rsOtherServices) > 0). This did the trick.
However, if I use the while (($row = mysqli_fetch_assoc($rsOtherServices)))... as suggested, even if there is a record, it doesn't display.
At this moment in time I'm not going to worry to much about that but will investigate it in the future.

Related

MYSQLI Prepared Statement White Page

I am trying to convert some PHP code for notifications into a more suitable Prepared statement. No matter what I try, it breaks my page. Is any body out there able to tell me where the error lies?
EDIT The page is not completely blank. The page breaks after this code.
$acctNotsQry = $redoDB->prepare('SELECT message, nDate FROM notifications WHERE uID = ? AND nSeen = "0" ORDER BY nDate DESC');
$acctNotsQry->bind_param('i', intval($memID));
$acctNotsQry->execute();
$acctNotsQry->store_result();
$acctNotsQry->bind_result($notMessage, $notnDate);
if($acctNotsQry->num_rows == 0){
echo '<li><div class="nilNots">NO NOTIFICATIONS</div></li>';
} else {
while($acctNotsQry->fetch()) {
?>
<li><i class="fa fa-bell"></i> <?php echo htmlspecialchars_decode(stripslashes($notMessage)); ?>
<p><?php echo date('d M Y - h:ia', strtotime($notnDate)); ?></p></li>
<?php
}
}
$acctNotsQry->close();
SECOND EDIT: The following code DOES work, the above does not. It might help with a solution:
$acctNotsQry = 'SELECT * FROM notifications WHERE uID = "'.$memID.'" AND nSeen = "0" ORDER BY nDate DESC';
$acctNotsRes = $redoDB->query($acctNotsQry);
$acctNotsNum = $acctNotsRes->num_rows;
if($acctNotsNum == 0){
echo '<li><div class="nilNots">NO NOTIFICATIONS</div></li>';
} else {
while($acctNotsRow = $acctNotsRes->fetch_assoc()){
$notMsg = $acctNotsRow['message'];
?>
<li><i class="fa fa-bell"></i> <?php echo htmlspecialchars_decode(stripslashes($notMsg)); ?>
<p><?php echo date('d M Y - h:ia', strtotime($acctNotsRow['nDate'])); ?></p></li>
<?php
}
}
I have tried searching for a solution to no avail.
Many thanks in advance.
Change
$acctNotsQry->bind_param('i', intval($memID));
to
$acctNotsQry->bind_param('i', $memID);
The 2nd and following arguments to bind_param are reference parameters, so you have to use variables there, not expressions. You don't need to call intval() yourself, the i type in the first argument tells mysqli to convert it to an integer when sending it to MySQL.

using if else with codeigniter return value

I am using codeigniter framework for one of my inventory management systems.
I prefer using php due to its variable functionality which makes things easier.
So I am trying to get the username from session and then check the database for the level of the user and accordingly he should be able to view links in the navigation menu.
$this->db->select('level');
$this->db->where('name', $this->session->userdata('name'));
$query = $this->db->get('admin');
if ($query) {
foreach ($query->result() as $row)
{
echo $row->level ;
}
}
This works fine and gets the level of the logged in user.
Now I want to use a if condition with this value, so I am using this -
if ($this->$row->level = '3') {
?>
<li class="">
Employee Sales Report
</li>
<?php } ?>
But nothing seems to happen here, No error or any notice.
I have no clue what I am doing wrong here.
if ($this->$row->level = '3') {
?>
<li class="">
Employee Sales Report
</li>
<?php } ?>
you shouldn't use = operation for equal. should use == or === for equal. by that code you put 3 $this->$row->level and make it's value 3.
if ($this->$row->level == '3') {
?>
<li class="">
Employee Sales Report
</li>
<?php } ?>

while loop not cycling through as expected

I have a loop that runs through returned rows from a MySQL query and performs commands.
When the data is displayed on the webpage (seen at the end of the pasted code), the requested_date (from the $result_dec query) is cycling through correctly, but the memPayout(from the $emailcompleted query) is not. Where it shows the memPayout it is just listing the same number for each date.
When I run this in phpmyadmin, I see that different dates have different memPayouts..so I know there is a bug here.
I appreciate the help.
UPDATE: I've updated the old code with the new one that I adjusted. I tried to wrap the email earnings query with the original foreach. I also removed the unnecessary extra bgcolor switch. The result on my webpage is still the same as before. Any more advice is really appreciated.
<?php
//--------------------- Code for Decline TRANSACTION --------------------------------------
$u_id = $_SESSION['user_ses']['id'];
$result_dec = #mysql_query("(select trv.tr_user_id , usr.full_name ,usr.email , usr.paypal , trv.requested_date, trv.requested_status, trv.click_payment_status, trv.tracking_id
from tbl_trackvalue as trv ,tbl_tracking as t , tbl_offers as off , tblusers as usr
where t.id=trv.tracking_id and off.id=t.offer_id and usr.id=trv.tr_user_id and usr.id='1454'
and trv.payment_status='pending' and trv.requested_status='declined' group by trv.tr_user_id, trv.requested_date order by trv.requested_date asc )
union all
(select trv.tr_user_id , usr.full_name ,usr.email , usr.paypal , trv.click_request_date, trv.requested_status, trv.click_payment_status, trv.tracking_id
from tbl_trackvalue as trv ,tbl_tracking as t , tbl_offers as off , tblusers as usr
where t.id=trv.tracking_id and off.id=t.offer_id and usr.id=trv.tr_user_id and usr.id='1454'
and trv.payment_status='pending' and trv.click_payment_status='declined' group by trv.tr_user_id, trv.click_request_date order by trv.requested_date asc ) ");
$nr = mysql_num_rows($result_dec);
$categories_d = array();
while($row = mysql_fetch_array($result_dec))
{
$categories_d[] = $row;
}
if(count($categories_d)>0)
{
$counter=0;
foreach($categories_d as $index=>$rec)
{
$counter++;
if ($counter % 2 == 0)
{
$bgcolor = "#FFFFFF";
}
else
{
$bgcolor = "#F5F7D9";
}
$userId=$rec['tr_user_id'];
$EmailCompletedOffer=#mysql_query("select off.member_amount as memPayout
from tbl_trackvalue as trv ,tbl_tracking as t , tbl_offers as off , tblusers as usr
where t.id=trv.tracking_id and off.id=t.offer_id and off.offer_type='mailchimp' and usr.id=trv.tr_user_id and
trv.tr_user_id=$userId and trv.requested_date='".$requested_date."' and trv.payment_status='pending' and trv.total_conversion !=0 and trv.requested_status='declined' ");
$rowemailEarn=#mysql_fetch_array($EmailCompletedOffer);
$totalEmailEarnAmount1=$rowemailEarn['memPayout'];
?>
<div class="datarow_his">
<div class="his_onecol1"><?php echo '$'. $totalEmailEarnAmount1;?> </div>
<div class="his_onecol1"> <?php echo "Declined"; ?> </div>
<div class="his_onecol2"><?php echo date("M j,Y " ,strtotime($rec['requested_date']));?></div>
<!--<div class="his_onecol1"><?php //echo date("M j,Y " ,strtotime($rec['paid_date']));?></div>-->
</div>
<p><?php echo $totalEmailEarnAmount1?></p>
<?php /*}
} */
}
}
?>
Your code roughly looks like this:
fetch categories
if(categories not empty) {
foreach(category) {
set $bgcolor and $userId (has no effect)
}
get query results for the last $userID and set $totalEmailEarnAmount1
if(categories not empty) {
foreach(category) {
set $bgcolor again (has no effect)
generate a div using $totalEmailEarnAmount1
}
}
}
Since you set $totalEmailEarnAmount1 outside the final foreach, it has the same value on every iteration of the loop.
(If you indent your code in a way that is consistent with its structure, errors like this will become obvious. The messy indentation makes it hard for you to see what is going on.)

Paginating MySql Results

I have a table that pulls tickets from a MYSQL database and displays them to the user. This part works wonderfully! But, once the user gets 5-10 tickets, the page becomes long and ugly. I am wanting to paginate the responses automatically to show maybe 5 tickets per page and show the rest on following pages.
I am using Bootstrap and have the following for the display of the pagination. "Although I fear this is only pseudo code" I added id's to each main element.
<ul class="pager" id="pagerUL">
<li class="previous" id="previousUL">
← Previous
</li>
<li class="next" id="nextUL">
Next →
</li>
</ul>
Essentially the only code used to pull the database information on the page is:
<?php if(count($tickets) > 0) : ?>
<?php foreach ($tickets as $ticket): ?>
//table content
<?php endforeach ?>
<?php else : ?>
<tr>
<td colspan="7">No Tickets Created.</td>
</tr>
<?php endif; ?>
I thought we could add something to the <?php if(count($tickets) > 0) : ?> But honestly, I am not sure as I am not a expert at php and never even attempted or had the need to build pagination up until now. Any help, guidance, thoughts would be appreciated.
Pass the page number thru the URL and then grab it via php with $_GET.
$page = $_GET['page'];
$per_page = 10; // Define it as you please
$start = $page*$per_page;
$query = "SELECT * FROM table LIMIT ".$start.", ".$per_page;
$result = mysql_query($query);
$count = mysql_num_rows($result);
$i = 0;
for($i=0;$i<$count;$i++)
{
//echo out what you want
echo "";
}
$total_pages = ceil($count/$per_page);
for($i=0;$i<$total_pages;$i++)
{
if($page != $i)
{
echo "<a href='/page.php?page=".$i."'>".$i."</a>";
} else
{
echo $i;
}
}

Removing Duplicate Entries from Join

I am running into a problem that I feel like is somewhat simple in principle, so I may not be going about this the correct way. I essentially am listing "events" on a page that have their own respective "comments". I am joining to two tables and hoping to run a foreach loop to get all of the events along with their comments. Unfortunately, if an event has more than one comment it's reprinting the original event because of the join (i suspect). What is a better way to achieve what I am trying to accomplish?
In my model:
function get_events()
{
$this->db->select('*');
$this->db->from('events');
$this->db->join('comments', 'comments.event_id = events.id', 'left');
$query = $this->db->get();
if ($query->num_rows() > 0)
{
return $query->result();
}
}
And in the view:
<?php foreach($events as $row): ?>
<div class="event">
<?= $row->event; ?>
<?php if($row->comment == null): ?><br>
<i>No comments.</i>
<?php else: ?>
<div class="comment">
<?= $row->comment; ?>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
In the db I basically have 3 events, one with 2 comments, one with just 1, and one with zero comments. The first event prints twice, each with a different comment, the second event prints fine with its own comment and the third one works perfectly, printing "no comments" since there is nothing joined with it.
Hopefully this all makes sense!
MT
You can check wether you already echoed the event:
<?php
$lastEvent = null;
foreach($events as $row) {
if($row->event != $lastEvent) {
echo '<div class="event">' . $row->event;
}
if($row->comment == null) {
echo '<br><i>No comments.</i>';
} else {
echo '<div class="comment">' . $row->comment . '</div>';
}
if($row->event != $lastEvent) {
echo '</div>';
}
$lastEvent = $row->event;
}

Categories