Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
My goal is to print all results (fetched from database) and print it according to respective formation.
In html, I could defined it as follows:
<div class="gallery-item category-birds">
<div class="gallery-item-image" style="background-image: url( '_uploads/bc7113a3956b7a45e926f30258fa1b83.jpg' );">
<div class="gallery-item-image-pattern">
<span>Show image</span>
</div>
</div>
<h3>ImageTest1</h3>
<p>MyImageDescription.</p>
</div>
In php, I am trying to print the results in a loop (like I have done before in others examples) but for some reason it's not working.
"animals.php"
<?php
include("../config.php");
$result = mysql_query("SELECT * FROM animals WHERE catid=2 ORDER BY id DESC",$connect);
while($myrow = mysql_fetch_assoc($result))
{//begin of loop
echo "<div class='gallery-item category-birds'>";
echo "<div class=gallery-item-image style='background-image:url(_uploads/bc7113a3956b7a45e926f30258fa1b83.jpg);'>";
echo "<div class=gallery-item-image-pattern>";
echo "<span>Show image</span>";
echo "</div>";
echo "</div>";
echo "<h3> ".$myrow['name']." </h3>";
echo "<p> ".$myrow['description']." </p>";
echo "</div>";
}//end of loop
?>
The index.php includes "animals.php".
The database connection is fine, because by accessing "animals.php" I have the content desired.
The index.php have css linked, where is defined the classes and others.
I have tried to replace inside of those echo's: " for \". -> Not working.
I have tried to replace inside of those echo's: " for '. -> Not working.
Could you help me? Iam out of ideas how to solve this.
Thanks and regards,
RMC
FIrst there is no issue with the MySQL if you can access the page and it works. The issue is when you include it in another page. The issue that comes to mind is if animals.php is located in a folder, example: /inc/. Your config.php and index.php are located in your docroot(/). This may cause a relative issue in loading the config.php file, because it may be looking out of your docroot. I would check your error.log on your server to verify this. I normally in any templated PHP website that I have created, my config.php file would be included in index.php since it would be used by the whole site.
A few potential solutions:
check your error.log to make sure it is finding the config.php file in the right location
move your include for config.php to index.php
read up on php templating, example tutorial - http://chadminick.com/articles/simple-php-template-engine.html
You're missing some single quotes around your class name of div 2 and 3
<?php
include("../config.php");
$result = mysql_query("SELECT * FROM animals WHERE catid=2 ORDER BY id DESC",$connect);
while($myrow = mysql_fetch_assoc($result))
{//begin of loop
echo "<div class='gallery-item category-birds'>";
echo "<div class='gallery-item-image' style='background-image:url(_uploads/bc7113a3956b7a45e926f30258fa1b83.jpg);'>";
echo "<div class='gallery-item-image-pattern'>";
echo "<span>Show image</span>";
echo "</div>";
echo "</div>";
echo "<h3> ".$myrow['name']." </h3>";
echo "<p> ".$myrow['description']." </p>";
echo "</div>";
}//end of loop
?>
(P.s. I also assume that you make your connection with the database in your config.php file,
what makes that you don't need to put your $connect variable in the mysql_query() function.
$result = mysql_query("SELECT * FROM animals WHERE catid=2 ORDER BY id DESC");
)
Related
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.
this code work only in admin page but can't work in view page how to retrieve image from view page in codelgniter
code:
<?php
include('config.php');
$result = mysql_query("SELECT * FROM photos");
while($row = mysql_fetch_array($result))
{
echo '<div id="imagelist">';
echo '<p><img src="'.$row['location'].'"></p>';
echo '<p id="caption">'.$row['caption'].' </p>';
echo '</div>';
}
?>
Why you use mysql extension in codeigniter ?!
Even though the meaning of that code work in admin is you store your image inside admin folder
So for best practice
Please use codeIgniter as a FrameWork not a place to paste your procedural PHP code !
Also show us what is in $row['location'] ?!
I am trying to put a variable into an AJAX-loaded page link I can call it on the content it loads?
I have tried:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav={$array['username']}', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»{$array['username']}</i>\n";
echo "</div>\n";
echo "</a><br />\n";
I tried putting the link inside and outside the php code but neither of them worked, I also tried a few differents ways of calling it including $array['username'] All I need is a way I can define the variable "nav" as username (which is set in a database) so I can use it on that content that is called by the link.
Is it even possible?
I am not sure I completely understood your question.
But perhaps you want to try this out:
<a href="#" onclick="ajaxpage('/content/staff/profile.php?nav=<?php echo $array['username'];?>', 'content')">
<?php
$i++;
echo "<div style=\"float:left; width:{$row_width}%;text-align:center;\">\n";
echo "<i>»", $array['username'], "</i>\n";
echo "</div>\n";
echo "</a><br />\n";
I am storing in a mySQL table the HTML/PHP content of individual slides to be displayed on a single page.
Here is an example of HTML/PHP code stored in the mySQL table:
<p>Welcome <?php echo $userData['fname']; ?>!</p>
<p>You made it to the first slide!</p>
I retrieve the content of the slides in PHP with the following code:
<?php
$fetchedPageSlideData = mysql_query("SELECT * FROM pageSlides WHERE pageID = $pageID ORDER BY 'order' DESC") or die(mysql_error());
while ($pageSlideData = mysql_fetch_array($fetchedPageSlideData)) {
$pageSlideContent = $pageSlideData['content']; ?>
<div><?php echo $pageSlideContent; ?></div>
<?php }
?>
All of the HTML of the content displays correctly, but the PHP is inserted as follows:
<!--?php echo $userData['fname']; ?-->
So the PHP is commented out and doesn't display.
How can I retrieve the HTML/PHP code and have the PHP not commented out?
It might be a better idea to use placeholder strings in the DB data. Executing arbitrary php code from a DB can be dangerous. PHP is Evil
Look into PHP function eval(): http://php.net/manual/en/function.eval.php
Dropping in and out of the PHP interpreter makes your code rather difficult to read. Consider:
<?php
$f = mysql_query(
"SELECT *
FROM pageSlides
WHERE pageID = $pageID
ORDER BY 'order' DESC"
) or die(mysql_error());
while ($d = mysql_fetch_array($f)) {
print "<div>" . $d['content'] . "</div>\n";
}
Regardless there is no implicit nor explicit mechanism here which would inject the comment tags you've presented. However it may be the browser trying to make sense of the unescaped html code and <?php ... ?> tags.
Try:
print "<div>" . htmlentities($d['content']) . "</div>\n";
As a side note, you might consider using
print "<div>" . highlight_string($d['content']) . "</div>\n";
Or do you mean that you actually want to run the code stored in the database - if so, you're asking for a world of pain. Eval is not evil - but you really must know what you're doing to avoid getting bitten by it.
I currently have the following code coming from a database table:
<h1 class="widgetHeader">My Friends</h1>
<div class="widgetRepeater">
<p class="widgetHeader">Random Selection</p>
<?php
$friends = $user->getFriends();
?>
<p class="widgetContent">
<?php
for ($i=0; $i<count($friends);$i++) {
$friend = $friends[$i];
?>
<span class="friendImage" style="text-align:center;">
<?php print $friend->username; ?>
</span>
<?php
}
?>
</p>
</div>
Now, ive tried using the eval function in php but i get a parse error unexpected '<'. I've also tried using the output buffer method (ob_start) without success too. Any ideas as to how i can get this code to evaluate without giving me an error?
note: the database code is stored in a variable called $row['code'].
The PHP eval function expects PHP code to execute as it's parameter, not HTML. Try enclosing your DB values with PHP close and open tags:
eval('?>' . $row['code'] . '<?php');
eval = evil!
Especially if the eval'd code comes from a db... one mysql injection = full php execution = full control.
Rather use some placeholders and replace them (like any other good templating system does).
You could store this in your database:
<h1 class="widgetHeader">My Friends</h1>
<div class="widgetRepeater">
<p class="widgetHeader">Random Selection</p>
{%friendstemplate%}
</div>
Then str_replace the placeholders with the content they should have. In your example i would also add a subtemplate per friend like this:
<span class="friendImage" style="text-align:center;">
{%username%}
</span>
... which you could loop and insert into {%friendstemplate%}.
You cant use eval on markup code. Either save the code to a temporary file so that you can include it, or rewrite the code so that it's not markup, something like:
print "<h1 class=\"widgetHeader\">My Friends</h1>";
print "<div class=\"widgetRepeater\">";
print "<p class=\"widgetHeader\">Random Selection</p>";
$friends = $user->getFriends();
print "<p class=\"widgetContent\">";
for ($i=0; $i<count($friends);$i++) {
$friend = $friends[$i];
print "<span class=\"friendImage\" style=\"text-align:center;\">";
print $friend->username;
print "</span>";
}
print "</p>";
print "</div>";