<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
<?php do { ?>
<?php echo $row_Recordset1['Name']; ?>:
<?php echo $row_Recordset1['Text']; ?>
•
<?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>
</marquee>
<?php mysql_free_result($Recordset1); ?>
Print a friendly message to the user instead of NULL:
<?php echo (NULL === $row_Recordset1['Text']) ? "No value" : $row_Recordset1['Text']; ?>
As xil3 illustrates, you can also use this pattern (from the docs):
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
The way you have it written right now, $row_Recordset1 will be null the first time it goes into the loop.
I've rewritten it for you:
<marquee behavior="alternate" scrolldelay="1" scrollamount="2">
<?php while($row_Recordset1 = mysql_fetch_assoc($Recordset1)) { ?>
<?php echo (($row_Recordset1['Name'] != null) ? $row_Recordset1['Name'] : 'n/a'); ?>:
<?php echo (($row_Recordset1['Text'] != null) ? $row_Recordset1['Text'] : 'n/a'); ?>
•
<?php } ?>
</marquee>
<?php mysql_free_result($Recordset1); ?>
Related
I have a database with products and categories tables. I have managed to show the product category and It's related products. And It's currently looks like following.
But I need this to be done like
Category B
Product 1
Category A
Product 2
Product 3
Product 4
Product 5
Following is my query
<?php
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<h1><?php echo $row["name"];?></h1>
<p><?php echo $row["prod_desc"];?></p>
<?php echo $row["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]; ?>
<?php
}
}
?>
Rearrange array first then try to use it for showing data
<?php
$final_array = [];
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)){
$final_array[$row["name"]][] = ['prod_desc'=>$row["prod_desc"],'prod_name'=>$row["prod_name"],'quantity'=>$row["quantity"],'price'=>$row["buy_price"]];
}
?>
<?php foreach($final_array as $key=>$val){?>
<h1><?php echo $key;?></h1>
<?php foreach($val as $v){?>
<p><?php echo $v["prod_desc"];?></p>
<?php echo $v["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]."<br><br>"; ?>
<?php } ?>
<?php } ?>
<?php }?>
Try to store last used category and check if it changed:
<?php
$category='';
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
if($category!=$row["name"]) {
?>
<h1><?php echo $row["name"];?></h1>
?>
}
$category=$row["name"];
<?php
<p><?php echo $row["prod_desc"];?></p>
<?php echo $row["prod_name"]; ?>
<?php echo $row["quantity"]; ?>
<?php echo $row["buy_price"]; ?>
<?php
}
}
?>
Remember to sort output data by category in SQL query
Here am checking an array values exist or not with another table and if exist it should produce the output.
Here is my array value which looks like this
the code i written is here
<?php if(array_search($v_employee->user_id,array_column($salary_info, 'user_id'))) {?>
<?php echo $v_employee->fullname; ?>
<?php } else { ?>
<?php echo $v_employee->fullname; ?>
<?php } ?>
it doesn't produce any result.please help me to solve.thanks in advance
array_column can't read object
try this one
<?php
$salary_info_arr=json_decode(json_encode(array_filter($salary_info)),true); //changed
if(array_search($v_employee->user_id,array_column($salary_info_arr, 'user_id'))) {?>
<?php echo $v_employee->fullname; ?>
<?php } else { ?>
<?php echo $v_employee->fullname; ?>
<?php } ?>
<?php } ?>
I'm trying to give structure to my code and i am facing a problem.
I'm looping through a sql query response and for each element i'm trying to retrieve other related elements. It works in my controller without problem but when i'm trying to repeat in the view I always get the same value for the related element
My controller:
<?php
include_once('class/guide.class.php');
$bdd = new DBHandler();
$req = guide::getGuides($bdd,0,5);
foreach ($req as $results => $poi)
{
$req[$results]['id'] = htmlspecialchars($poi['id']);;
$req[$results]['name'] = nl2br(htmlspecialchars($poi['name']));
$guide = new guide($results['name'],$bdd);
$guidePois = $guide->getGuidePois($poi['id']);
foreach ($guidePois as $res => $re)
{
echo $guidePois[$res]['id'];
echo $guidePois[$res]['name'];
$guidePois[$res]['id'] = htmlspecialchars($re['id']);
$guidePois[$res]['name'] = nl2br(htmlspecialchars($re['name']));
}
}
include_once('listing.php');
here, you see that I echo the ids/names of the related list of element and it works well, the output is correct for each element of the first list.
When i do it in my view:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Somehow the first list output are the good elements, but for the 2nd list, i always get the related elements of the first item.
Do you have an idea ?
Thanks a lot for your help
This is because you only set:
$guidePois = $guide->getGuidePois($poi['id']);
once in the controller.
If you want it to work in the view, you need to insert this code right after the closing </h3>
<?php $guidePois = $guide->getGuidePois($poi['id']); ?>
So that $guidePois gets a new value in each iteration.
Complete view code:
<?php
foreach($req as $poi)
{
?>
<div class="news">
<h3>
<?php echo $poi['id']; ?>
<em>: <?php echo $poi['name']; ?></em>
</h3>
<?php
$guidePois = $guide->getGuidePois($poi['id']);
foreach($guidePois as $re)
{
?>
<h4>
<?php echo $re['id']; ?>:
<?php echo $re['name']; ?>
</h4>
<?php
}
?>
</div>
<?php
}
?>
Please could someone explain to me how could I adapt my code to make it so that if a record / value doesnt exist in the mysql table it will echo a piece of text? Thank you.
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
?>
Use the ternary operator '?:'
Sample:
$you_var ?: 'you_text_if_not_exists'
Check your variables like that :
<?php (isset($reviews['display_name']) ? $reviews['display_name'] : "entry doesn't exists"; ?>
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews = mysql_fetch_array($reviews_set))>=1)
{
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
}
} else {
echo 'No reviews available';
}
?>
If you want to check if table has rows, use following:
$num_rows = mysql_num_rows($reviews_set);
$num_rows will contain number of rows.
<?php
$reviews_set = get_reviews();
?>
<h3>Latest Reviews</h3>
<?php
if(mysql_num_rows($reviews_set) > 0) {
while ($reviews = mysql_fetch_array($reviews_set)) {
?>
<div class="prof-content-box" id="reviews">
<div class="message_pic">
<?php echo "<img width=\"50px\" height=\"50px\" src=\"{$prof_photo}\">";?>
<?php echo "<strong>Review from {$reviews['display_name']}:</strong><br /><br/> {$reviews['content']} <br />";
<?
}
}else{
echo "No Data";
}
?>
I hope this helps
I am retrieving a column from my database and storing that values in an array in my controller.
I am passing that array to the view page, and displaying that values to the user.
All things upto this are working fine.
But the problem is when I am refreshing the page its not showing the values obtained by the controller from the model and just showing blank page..\
I also tried to store the value in a session and use that value but that doesn't seem to work for me.. :(
here is my controller code :-
function full_post_view(){
$data= array(
'ticket_id' => $this->input->post('ticket_id')
);
$this->session->set_userdata($data);
$ticket_id = ($this->input->post('ticket_id')) ? $this->input->post('ticket_id') : $this->session->userdata('ticket_id');
// $post_content = $this->session->userdata('post_content');
echo $ticket_id;
$this->load->model('helpdesk_model');
$all_comments = $this->helpdesk_model->fetchComments($ticket_id);
$is_post_closed = $this->helpdesk_model->fetchPostStatus($ticket_id);
if($all_comments->num_rows > 0) {
foreach ($all_comments->result() as $comments_value) {
$comments = $comments_value->comments;
}
$count=1;
Template::set('all_comments',$all_comments);
Template::set_view('helpdesk/full_post_view');
}
else {
$count=0;
Template::set('post_content',$this->input->post('post_content'));
}
Template::set('is_post_close',$is_post_closed);
Template::set('ticket_id',$ticket_id);
Template::set('is_post_closed',$is_post_closed);
Template::set('post_content',$post_content);
Template::set('count',$count);
Template::set('is_post_closed',$is_post_closed);
Template::render();
}
here is my view :-
<?php $post=Template::get('post_content'); ?>
<?php $ticket_id=Template::get('ticket_id'); ?>
<?php $is_close = Template::get('is_close'); ?>
<h4>Your Problem :- </h4>
<?php echo $post; ?>
<hr/>
<?php foreach ($is_post_closed->result() as $value) {
$is_close = $value->is_close;
} ?>
<?php if(Template::get('count') > 0) : ?>
<?php foreach($all_comments->result_array() as $commentsRow) : ?>
<?php echo word_wrap($commentsRow['comments'],15); ?>
<?php echo " by->"; ?>
<?php echo $commentsRow['username']; ?>
<?php echo $commentsRow['role_name']; ?>
<hr/>
<?php endforeach; ?>
<?php else : ?>
<br/>
No Comments Yet
<?php endif; ?>
<?php if($is_close == 0 ) : ?>
<?php echo form_open('helpdesk/newComment'); ?>
<?php echo form_hidden('ticket_id',$ticket_id); ?>
<?php echo form_hidden('post_content', $post); ?>
<?php echo form_textarea('comment_from_user'); ?>
<?php echo form_submit('submit', 'Comment '); ?>
<?php echo form_close(); ?>
<?php endif; ?>
<?php if($is_close==0) : ?>
<?php echo form_open('helpdesk/closePost'); ?>
<?php echo form_hidden('ticket_id', $ticket_id); ?>
<?php echo form_hidden('post_content', $post); ?>
<?php echo form_submit('submit','close post'); ?>
<?php echo form_close(); ?>
<?php else : ?>
<?php echo form_open('helpdesk/reopenPost'); ?>
<?php echo form_hidden('ticket_id', $ticket_id); ?>
<?php echo form_submit('submit','Reopen post'); ?>
<?php echo form_close(); ?>
<?php endif; ?>
Edit :
I want to ask that how to load the database content again to the view when user refreshes the page.
Edited my Controller and view code
When you refresh the page, do you resend the form ? I'm asking this because from your controller code, it looks like $ticket_id is fetched from _POST and you use pass this variable to the model. If you don't resend the form, the $ticket_id is null and nothing is fetched from DB.
Edit:
Replace in your controller:
$ticket_id = $this->input->post('ticket_id');
with:
$ticket_id = ($this->input->post('ticket_id')) ? $this->input->post('ticket_id') : $this->session->userdata('ticket_id');
and make sure on each request ticket_id exists at least in one from the two possibilities (POST and SESSION)
Solved :)
the problem was when I was refreshing the page, ticket_id was set to 0 and that value was passed as a post .
So the session value was the new ticket_id i.e., 0 and so it was displaying nothing..
I did this:-
if(isset($_POST['ticket_id'])) {
$data= array(
'ticket_id' => $this->input->post('ticket_id'),
'post_content' => $this->input->post('post_content')
);
$this->session->set_userdata($data);
}
$ticket_id =$this->session->userdata('ticket_id');
$post_content =$this->session->userdata('post_content');
i.e., I am storing ticket_id in session when post in set i.e., for the first time. When page is refreshed next time ticket_id post is not set and data is taken from session.