Using foreach loop to create links from an array [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Please help me create links using a php foreach loop that iterates over an array which contains the names of navbar links for a webpage.
Currently my loop creates links but when you click on them an error 404 page and displays in the url (for example when clicking on "blog"):
...homebrew-actual/blog.php>Blog <a></li><li><a href="
I would like the url to go to:
... homebrew-actual/blog.php
without the html tags.
Here is my current PHP loop:
<nav>
<ul>
<?php
$navOptions = array('index', 'showcase','about','blog','contact','forums');
foreach($navOptions AS $navOption) {
if ($navOption == $currentPage) {
print '<li>' . '' . ucfirst($navOption) . '</li>';
} else {
echo '<li>' . '<a href="/homebrew-actual/' . $navOption . '.php>' . ucfirst($navOption) . '</a></li>';
}
}
?>
<li class="special">Shop</li>
</ul>
</nav>
Please help me identify a solution to create a links for a navbar using an array with the link names and use a for loop to link to those pages.
Thank you for checking out this question.

You forgot a ":
print '<li>' . '' . ucfirst($navOption) . '</li>';
^--start href ^---end of href, missing "
Since you never close the href string, you end up with broken HTML.

Related

How can i insert if statement to variable in php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to make a dynamic tab with PHP. please can someone help me out just need to filter where the category parent = 0.
$parent_category_html.= '<span class="'.$current_tab.'"><a id="tab-'
. $category['id'].'-link" href="#tab-'.$category['id']
. '" role="tab" aria-selected="false" aria-controls="tab-1" class="tab-link">'
. **$category['title'] is it possible to put an if statement here to filter the title where parent = 0**
. '</a>
From comments on the question, it sounds like what you're asking is:
How can I conditionally include a value in my string?
For that you'd use the ternary conditional operator. The operation itself would look like this:
$parent == 0 ? $category['title'] : ''
It essentially means:
If $parent equals 0, $category['title'], else empty string
The whole operation resolves to the resulting value, so you can wrap the whole operation in parentheses and drop it anywhere you would a variable. So something like this:
$parent_category_html.= 'your various HTML code...'
. ($parent == 0 ? $category['title'] : '')
. 'the rest of your HTML code...';
You can add if statement before:
if (statement)
$parent_category_html .= '...';
or try to use ternary operator
$parent_category_html .= "<div>" . ($condition ? 'output if true' : 'output if false') . "</div>";
$parent_category_html.= '<span class="'.$current_tab.'"><a id="tab-' . $category['id'].'-link" href="#tab-'.$category['id'] . '" role="tab" aria-selected="false" aria-controls="tab-1" class="tab-link">';
if ($pareent_id ==0) {
$parent_category_html.= '...';
}
$parent_category_html.= '</a>';

Trying to make this entire line of code into a hyperlink [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
echo <a href = 'test.php'> "CategoryID: " . $row["CategoryID"]. " - Category Name: ".$row["CategoryName"]. </a> "<br>";
This is what i have an is not working properly.
This:
echo "<a href = 'test.php'>CategoryID: {$row['CategoryID']} - Category Name: {$row['CategoryName']}</a><br />";
I am using the { and } as they allow you to include an array in a string and ignore the concatenation which I find harder to read.
I find it funny that you can loop through a MySQL array but can't echo a simple string :P
Some links (teach a man to fish...):
W3Schools
PHP documentation
Codecademy
Tutorials Point
Try this:
<?php
$link = "";
$link = sprintf("<a href = 'test.php'>CategoryID: %d - Category Name: %s </a><br />", $row['CategoryID'], $row['CategoryName']);
echo $link;
?>
Assuming that $row['CategoryID'] is an integer and $row['CategoryName'] is a string.

using <a href> for a mysqli select statement [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
could someone please direct me to a simple (basic easy) tutorial or resource page of how to use hyperlinks in a website to display the results of a select statement
To be precise - I am working on the footer section of my website http://www.mandyevansartist.com
when someone clicks on the words 'coloured pencils' I would like them to be taken to the catagory.php page and be shown an array from the database
I know this is possible because I already have made it work by clicking an image - (there is an area in the http://www.mandyevansartist.com/gallery.php page that says 'click on a picture to see others in that catagory')
I have achieved this by
<?php
session_start();
include 'header.php';
echo '<h1>CLICK ON AN IMAGE TO SEE OTHERS IN THAT CATAGORY</h1>';
$con = mysqli_connect("*","*","*","*");
$db = mysqli_select_db("images", $con);
$answer = mysqli_query($con,"SELECT image FROM images where HEAD = 'true'");
echo '<div id = "list">';
echo '<ul>';
while ($row = mysqli_fetch_array($answer)) {
$pic = $row[image];
$link ="<a href = 'catagory.php?id=".$row[image]."'>" . ' <img src="'.$pic.'" style ="height:222px;"/> '. "</a>";
echo '<li>' .$link.'</li>';
}
echo '</ul>';
echo '</div>';
?>
At this point my footer.php is mainly html (enclosed in an echo'') with links going to nowhere
<div id="footer-one">
<h1>GALLERY</h1>
<p><a href = "#" >people pictures</a></p>
<p><a href = "#" >romance</a></p>
<p><a href = "#" >seascapes</a></p>
<p><a href = "#" >under the ocean</a></p>
<p><a href = "#" >paintings</a></p>
<p><a href = "#" >love heart series</a></p>
<p><a href = "#" >new works</a></p>
</div><!--/footer-one-->
one of the ways I have tried to insert a mysqli query into it
<h1>GALLERY</h1>';
$answer=mysqli_query($con,"SELECT image FROM images WHERE catagory = pencils");
while ($answer2 = mysqli_fetch_array($answer));
$link = <p>family portraits</p>
echo '<p>' .$link.'</p>';
echo' <p><a href = "#" >coloured pencils</a></p>
which just comes up with a parse error - unexpected "<"
I am looking for a tutorial to guide me through this process because i have looked and looked and cant find one
The error parse error - unexpected "<" is due to some missing stuff like ', " and {}. Try following fixes.
<?php
$answer = mysqli_query($con,"SELECT image FROM images WHERE catagory = pencils");
while ($answer2 = mysqli_fetch_array($answer));
{
$img = $answer2['image'];
$link = "<a href = 'catagory.php?id=$img'>family portraits</a></p>";
}
echo '<p>'.$link.'</p>';
echo '<p><a href = "#" >coloured pencils</a></p>';
?>
From the given code, I have come up with these fixes. But there may be some changes as per your requirements.

php string two echo statements together [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
this string is breaking my code and I am not sure why. If anyone can help me correct it I would appreciate it.
if ($p == $url) { echo '<li><a class="active" href="index.php?p=' . $url . '">'
. $label . '</a></li>'; } else { echo '<li>' . $label . '</li>'; }
htmlspecialchars(urlencode($url)) and htmlspecialchars($label). The rest seems fine.

php echo div class not working [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to echo a div class <div class="Box"> but not working. Can anyone help? Thanks.
echo '<div class="Box">
echo Anchor(T('Edit My Account'), '/profile/edit', FALSE, array('class' => 'Popup EditAccountLink'));
echo "<br />";
echo Anchor(T('Change My Password'), '/profile/password', FALSE, array('class' => 'Popup PasswordLink'));
echo "<br />";
$Inbox = 'Inbox';
$CountUnreadConversations = $Session->User->CountUnreadConversations;
if (is_numeric($CountUnreadConversations) && $CountUnreadConversations > 0)
$Inbox .= ''.$CountUnreadConversations.'';
echo Anchor(T('Inbox'), '/messages/all', 'Inbox');
</div>'
Your first and last lines are wrong, missing proper quotation marks and also an echo on the last line. It should be:
echo '<div class="Box">';
.... rest of code here...
echo '</div>';
You haven't terminated the string with ';.
echo '<div class="Box">';
Enable the error_reporting to E_ALL. Read on documentation. After this, you know what is happen.
This is a parse error. Enable error_reporting to E_ALL.

Categories