Display MySQL request in PHP - php

I am trying to make a basic todolist but i want to display a list with task undone at the top and the done at the bottom.
Every time i try i have a blank page
<h1 class="header">To do.</h1>
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<?php if ( $item['done'] == 0 ) {
echo '<li>';
echo '<span class=\"item'?><?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> No tasks</p>
I have a table items with a field Done (0,1)
I have tryed to put if condition in the foreach but it failed.
Thanks for the help

At the end of your line which begins with echo '<span class=\"item'?> you set a PHP open tag but without closing it and then open another one on the next line:
<?php echo $item['name']; c</span>
<?php if (!$item['done']): ?>
It looks like you meant to close the PHP tag on the previous line with ?>.
As a side suggestion, if you use a good editor or IDE it will show you errors on front of you as you type, saves a lot of time.
Viewing your PHP error logs is a must as a developer, as it tells you of problems as you work, and this would have shown a fatal error and the line number etc.
A white page is also a sign that you have a fatal error as PHP crashes before it will output any content to the screen.

When i remove the if
The code is working
<?php if (!empty($items)): ?>
<ul class="items">
<?php foreach ($items as $item): ?>
<li>
<span class="item<?php echo $item['done'] ? ' done' : '' ?>"> <?php echo $item['name']; ?> </span>
<?php if (!$item['done']): ?>
Mark as done
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php else: ?>
<p> You have no task</p>

Related

Working with multiple breadcrumbs in a knowledge base

I am working with Heroic Knowledge Base, and I am having trouble getting only one breadcrumb to show up (their suggested CSS code is not working). Unfortunately, I am not a PHP expert, and I am not sure how to edit the code so that it only shows one of the lines that it is pulling (we don't care which one) I have added the PHP from their breadcrumbs file. Any assistance would be greatly appreciated.
<?php
/**
* Breadcrumbs template
*/
?>
<?php if(hkb_show_knowledgebase_breadcrumbs()): ?>
<!-- .hkb-breadcrumbs -->
<?php $breadcrumbs_paths = ht_kb_get_ancestors(); ?>
<?php foreach ($breadcrumbs_paths as $index => $paths): ?>
<ol class="hkb-breadcrumbs" itemscope
itemtype="http://schema.org/BreadcrumbList">
<?php $last_item_index = count($paths)-1; ?>
<?php foreach ($paths as $key => $component): ?>
<li itemprop="itemListElement" itemscope
itemtype="http://schema.org/ListItem">
<?php if($key==$last_item_index): ?>
<span itemprop="item">
<span itemprop="name"><?php echo
$component['label']; ?></span>
</span>
<?php else: ?>
<a itemprop="item" href="<?php echo $component['link']; ?>">
<span itemprop="name"><?php echo $component['label']; ?></span>
</a>
<?php endif; ?>
<meta itemprop="position" content="<?php echo $key+1; ?>" />
</li>
<?php endforeach; ?>
</ol>
<?php endforeach; ?>
I was able to solve this issue using the following code, however I would like to make it so that it is only effective when there is more than one.. the code below worked on articles with two breadcrumbs, but when there is only one it hid them all together.
ol.hkb-breadcrumbs {
display: table-column-group;
}

Php Session issue?

I have stored an error message in session variable and it gets displayed on page when condition becomes false. Now, i want that on refreshing the page the message disappear thus , freeing up the Session Variable.
Code : To show the error message
<?php
if (isset($_SESSION['er'])): //Showing Errors on Salary form ?>
<div class="form-errors">
<?php foreach ($_SESSION['er'] as $mistake): ?>
<p> <?php echo $mistake ?> </p>
<?php endforeach ;?>
</div>
<?php endif; ?>
Storing the error
$_SESSION['er'] = array("Salary Must be between 10000 and 80000.");
header("Location:addSalary.php");
Just unset it right after you printed error. So it will be shown only once
<?php if (isset($_SESSION['er'])): //Showing Errors on Salary form ?>
<div class="form-errors">
<?php foreach ($_SESSION['er'] as $mistake): ?>
<p> <?php echo $mistake ?> </p>
<?php endforeach ;?>
</div>
<?php unset($_SESSION['er']); ?>
<?php endif; ?>
Then once you have used/sent the error message you can remove it.
<?php
if (isset($_SESSION['er'])): ?>
echo '<div class="form-errors">';
foreach ($_SESSION['er'] as $mistake):
echo "<p>$mistake</p>";
endforeach;
echo '</div>';
unset($_SESSION['er']);
endif;
?>

PHP echo as condition inside PHP if statement

I would like to use a PHP echo as a condition inside a PHP if statement.
The aim is to have the list of blog articles written by John Doe, displayed on his biography page.
It worked when I directly wrote the author's name in the if condition:
<!-- current page: biography page -->
<div id="list_of_articles_by_John_Doe">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == 'John Doe'): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
But I would like to automate the process, for each writer's biography page to have their own list of articles.
I tried to have as a condition the author of the current biography page ($page):
<!-- current page: biography page -->
<div id="automatic_list_of_articles">
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
but it makes another issue: it does not work because inside the foreach statement, $page->author() (condition in the if statement) does not echo the author once, but one time for each page('magazine')->children() as $article.
The condition if($article->author() == $page->author()) does not work in this case, as $page->author() is not strictly the writer's name.
I'm wondering how to call $page->author() to echo the writer's name only once, when inside the foreach statement.
What could be an option is to save all author within an array
// if article->author() isn't within the array
$authors[] == $article->author();
After that you could go as the following:
<?php foreach($authors as $author){ ?>
<?php foreach(page('magazine')->children() as $article): ?>
<?php if($article->author() == $author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
<?php } ?>
That should work, even if you must do 2 foreachs
<?php if( $article->author() == $page->author() ) { ?>
<p><?php echo $article->title(); ?></p>
<?php } ?>
should work, but you can also try
<?php
if( $article->author() == $page->author() ) {
echo "\n<p>", $article->title(), "</p>\n";
}
?>
which to me looks "cleaner"; but you'd have to have a look for missing whitespaces
I suggest trying to set it equal too a variable and then using that variable in the if statement.
<?php foreach(page('magazine')->children() as $article): ?>
<?php $condition = $page->author()?>
<?php if($article->author() == $condition ?>'): ?>
echo "\n<p>", $article->title(), "</p>\n";
<?php endif ?>
<?php endforeach ?>
I am not sure if my syntax is correct but i think it is something along them lines.
You cannot use echo in condition because it is special language construct that sends given contents to the output stream and it returns no value.
Are you sure you shouldn't have this?
<div id="automatic_list_of_articles">
<?php $page = page('magazine'); ?>
<?php foreach($page->children() as $article): ?>
<?php if($article->author() == $page->author()): ?>
<p><?php echo $article->title() ?></p>
<?php endif ?>
<?php endforeach ?>
</div>
I have reconstructed an approximation of what looks to be your data, and you can see it working at the link below. It correctly echo's multiple article titles.
Working example:
http://ideone.com/jvLVhF
In this example you can see the PHP as above works correctly, and it is likely a data issue (ie. you should perhaps be using $page and not calling a function in the foreach statement).

Error with php switch function

I´ve got the following code and when I paste the code to my page, I get a white page, can you guys help me?
PHP
<?php $strRendersettings = ($this->settings)? 'settings' : 'view'; ?><?php if (count($this->data)): ?>
<ul>
<?php
switch($_SERVER['HTTP_HOST'])
{
case ("www.domain.de"):
foreach ($this->data as $arrItem): ?>
<li class="new_doc_home"><a href=""; ?>" ><strong><?php echo $arrItem['text']['new_docs_titel']; ?>:</strong><br>
<p><?php echo $arrItem['text']['new_docs_Text']; ?></p></a>
</li>
<?php
break;
case ("www.domain.com"):
foreach ($this->data as $arrItem): ?>
<li class="new_doc_home"><a href=""; ?>" ><strong><?php echo $arrItem['text']['new_docs_titel']; ?>:</strong><br>
<p><?php echo $arrItem['text']['new_docs_Text']; ?></p></a>
</li>
<?php
break;
case ("www.domain.fr"):
foreach ($this->data as $arrItem): ?>
<li class="new_doc_home"><a href=""; ?>" ><strong><?php echo $arrItem['text']['new_docs_titel']; ?>:</strong><br>
<p><?php echo $arrItem['text']['new_docs_Text']; ?></p></a>
</li>
<?php
break;
}
?>
<?php endforeach; ?>
</ul>
<?php else: ?>
<?php endif; ?>
I need the code because of some different top level domains.
Thanks :)
The porblem is that you never end you foreach loops.
You should end each foreach: loop with endforeach;. Now you have 3 loops and only one closing endforeach;
For example this code works without a problem:
<?php
$array = [1=>2, 3=> 4];
$array2 = [5=>6,7=>8];
foreach ($array as $k => $v):
foreach ($array2 as $i => $j):
echo $i.' '.$j."<br />";
endforeach;
endforeach;
while the following doesn't:
<?php
$array = [1=>2, 3=> 4];
$array2 = [5=>6,7=>8];
foreach ($array as $k => $v):
foreach ($array2 as $i => $j):
echo $i.' '.$j."<br />";
//endforeach;
endforeach;
I advice you also to add at the beginning of your PHP file:
<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
to have all warnings and error displayed. Of course on production you should disable error reporting
It looks like you're breaking the foreach loop with the break of the switch. You should end the foreach inside each case of the switch.
See you PHP error log for error messages. When there is a parse error, the page is shown blank, and the error message goes to the PHP error log.
In your code, <?php endforeach; ?> appears to be outside the switch. I guess each foreach should have its own endforeach before the switch's break.
Correct me if I'm wrong, but you are doing the exact same thing for each of the three scenarios. I would alter my code to this:
<?php $strRendersettings = ($this->settings)? 'settings' : 'view'; ?>
<?php if (count($this->data)): ?>
<ul>
<?php
switch($_SERVER['HTTP_HOST'])
{
case "www.domain.de":
case "www.domain.com":
case "www.domain.fr":
foreach ($this->data as $arrItem): ?>
<li class="new_doc_home">
<a href="">
<strong>
<?php echo $arrItem['text']['new_docs_titel']; ?>:
</strong><br>
<p>
<?php echo $arrItem['text']['new_docs_Text']; ?>
</p>
</a>
</li>
<?php
endforeach;
break;
}?>
</ul>
You need to add endforeach statement before every break statement. Add that and your error will get sloved.
Hope this help :)

Warning: Invalid argument supplied for foreach() (deleted wordpress menu) PHP issue or WP issue?

I deleted a menu in my WP CMS. This was a menu in the footer.
Now on my sites footer, I get this text.
Warning: Invalid argument supplied for foreach() in /home/user/example.net/wp-content/themes/example/sidebar-footerfour.php on line 11
I tried restoring the menu but the text is still there.
Here is the code in my WP editor of sidebarfooterfour
<div class="fwidth185 lefts"><h4>UK Payday <span> Lenders</span></h4>
<?php $trusted_loan = wp_get_nav_menu_items('UK Payday Lenders'); $count = 1; ?>
<ul class="loan_cat">
<?php foreach ($trusted_loan as $menu_loan) : ?>
<li><?php echo $menu_loan->title; ?></li>
<?php endforeach; $trusted_loan=''; ?>
</ul>
<h4>Responsible <span> Lendings</span></h4>
<?php $trusted_loan = wp_get_nav_menu_items('Responsible Lendings'); $count = 1; ?>
<ul class="loan_cat">
<?php foreach ($trusted_loan as $menu_loan) : ?>
<li><?php echo $menu_loan->title; ?></li>
<?php endforeach; ?>
</ul>
</div>
I don't know code so I am not sure what to do or even whether it's a Wordpress issue or PHP issue. Any thoughts?
Make a backup of your file. After that clear the file and add the following:
<div class="fwidth185 lefts">
<h4>UK Payday <span> Lenders</span></h4>
<?php $trusted_loan = wp_get_nav_menu_items('UK Payday Lenders'); $count = 1; ?>
<ul class="loan_cat">
<?php foreach ($trusted_loan as $menu_loan) : ?>
<li><?php echo $menu_loan->title; ?></li>
<?php endforeach; $trusted_loan=''; ?>
</ul>
</div>
This will get rid of the error and remove any leftover HTML associated with the menu you have deleted.
Instead of just using foreach($trusted_loan as $menu_loan), try using this contruct instead:
foreach($trusted_loan ?: array() as $menu_loan)
(Or, of you have PHP 5.4 or greater, use $trustedload?:[])
This will ensure that if $trusted_loan is not set or otherwise falsy, you will be using an array and avoiding the error.

Categories