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.
Related
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.
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 6 years ago.
Improve this question
my HTML code is THE CODE IS REPEATED 16 times :
<div class="headline_image">
<a ga-cat="slideshow-view" ga-action="view-1" href="mylink"><img src="http://dd4994.jpg" width="420" height="323" align="right" alt="my text "/></a>
</div>
I WANT TO GET all the imgs links and text also href what i did :
for ($x = 0; $x <= 15; $x++) {
$imglink = $html->find('div[class=headline_image] img', $x)->getAttribute('src');
$mytext = $html->find('div[class=headline_image] img', $x)->getAttribute('alt');
$postlink = $html->find('div[class=headline_image] a', $x)->getAttribute('href');
echo '<br/>';
echo $mytext;
echo '<br/>';
print_r($postlink);
echo '<br/>';
}
the code is slow any changes ?
You slow down your code by using too much anonymous objects. It means you don't put the result of the function into a variable, rather just use it "on the go". This needs to run your function again and again slowing down your project.
Because you can use the function find to return an array, I advice you to do so before the for loop.
$imgarray = $html->find('div[class=headline_image] img', $x);
This way you run $html->find exactly once, and not sixteen times. In the for loop you can use it as an array and work with the results: $imgarray[$x]. You make the same for $anchorarray and your code will speed up, you'll see.
Alternative solution is using PHP DOM $childNodes on the container in which this 16 item can be found (or the body element). This will return the sixteen div elements in which you can navigate by calling $firstChild for the <a> element and $firstChild again for the <img> element. Probably this is more secure in case you want to make changes to the website (like adding more content to the end etc.)
Hey Daniel i changed the code to :
$imgarray = $html->find('div[class=headline_image] img');
$linkarray = $html->find('div[class=headline_image] a');
for ($x = 0; $x <= 15; $x++) {
echo $imgarray[$x]->getAttribute('src');
echo '<br/>';
echo $imgarray[$x]->getAttribute('alt');
echo '<br/>';
echo $linkarray[$x]->getAttribute('href');
echo '<br/>';
}
In general the proper way to iterate looks like this:
foreach($html->find('div') as $div){
echo $div;
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I wrote this code
<html>
<head>
<title> page 1</title>
<body>
<style>
a{
margin-left:10px;
}
</style>
<?php
$con = mysqli_connect('localhost', 'root', '');
mysqli_select_db($con,"uoh");
$q = " SELECT * FROM student WHERE id = 201102820" ;
$result = mysqli_query($con , $q ) ;
if($row = mysqli_fetch_array($result)){
if ($row["major"]=="computer engineerig"){
echo "welcome ". $row["name"];
echo '<img src="tran.png"/>';
}
}
?>
</body>
</html>
but when I run it show me like this
Welcome stephen ICON
the icon(picture) that I put comes in front of the text .
Can I do like this
welcome stephen
ICON
I want the icon (picture) comes under the text.
HTML have a tag called Break :) you should echo this :
echo "welcome ". $row["name"];
echo "<br />";
echo '<img src="tran.png"/>';
you need a line break
echo "welcome ". $row["name"] . '<br/>';
echo '<img src="tran.png"/>';
or
echo '</p>' . "welcome ". $row["name"] . '</p>';
echo '<img src="tran.png"/>';
<a> and <img> are inline elements by default. See this article : CSS display: inline vs inline-block
So you have to put the ICON block in a block element to see a separation between the name and the icon.
Like this for example :
if ($row["major"]=="computer engineerig")
{
echo "welcome ". $row["name"];
echo '<p><img src="tran.png"/></p>';
}
Or adding a <br/> like suggested #Noor Adnan
<?php
//here you can add php
?>
<p>welcome <?php echo $row["name"]; ?> </p>
<br />
<img src="tran.png"/>
<?php
// here you can add your php
?>
If you Separate your HTML and PHP then you can easily add css and HTML inside your PHP.
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 7 years ago.
Improve this question
I'm trying to save a string containg a variable to another variable
$bg-color = '#aaa';
$bg_style = 'background: {$block_bg_color_top};';
<div class="block" style="<?php echo $bg_style; ?>">
</div>
I'm trying to echo the code in the style tag like this.
<div class="block" style="background: #aaa;">
</div>
You are trying to evaluate a variable in your background: ... string. But variables inside single quotes aren't evaluated; they have to be in double quotes. Also, you don't need the { and } in this context. Change your code like this:
$bg_style = "background: $block_bg_color_top;";
You could also do $bg_style = 'background: ' . $block_bg_color_top . ';';. In this instance, the two statements are equivalent.
I'm assuming you set $block_bg_color_top somewhere; it's not in the code you posted. You tried to define another variable, $block-bg, but that is not a valid variable name, as explained below.
This is not strictly part of the answer, but please note that you also have a syntax error in your code. This line will result in an error:
$bg-color = '#aaa';
You will get the error:
PHP Parse error: syntax error, unexpected '=' in ...
This is because $bg-color is not a valid variable name; it looks to the parser like you are subtracting color from $bg, but you can't assign to the result of an expression, just like you couldn't do $x - 5 = 7;.
What you're trying to do is the following :
$bg-color = '#aaa';
$bg-style = 'background: '.$bg-color;
<div class="block" style="<?php echo $bg_style; ?>">
</div>
I think this is what you want:
$bg_color = '#aaa';
$bg_style = 'background: ' . $bg_color . ';';
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.