Retrieve image from database using CodeIgniter - 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'] ?!

Related

echo images using php

what am i trying to do is echo an image using php,
my code is really simple..
i have stored the path of the image in mysql db..
the path of the image is: ../users/profiles/23/images/dps/1409947526.jpg
now i am using the following code to output this picture:
mysql_connect("localhost", "root", "") or die("error!");
mysql_select_db("xone");
$query = mysql_query("SELECT * FROM userdpcover WHERE id='23'");
$result = mysql_fetch_array($query);
$dir = $result['dp_address'];
$dp_name = $result['dp_name'];
$dp = $dir.$dp_name;
echo $dp;
echo "<img src='$dp' />";
but when i run this code, all i get is an broken image!
thanks in advance!
Have you tried this?
echo "<img src='".$dp."' />";
Please try this and please check your image path url correct....
<?php
//here your code
?>//close php tag and try this ...
<img src="<?php echo $dp; ?>"/>
<?php
//your code here
?>
View the source of the output HTML. This will tell you the image path. You probably need to modify it.
Also, remember to set your base URL in a PHP config file to avoid repeating URL paths.

How to php echo content into <div> classes dynamicaly [closed]

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");
)

Display Buddypress Profile Field

Using the latest version of Wordpress & Buddypress, I am trying to display a certain custom profile field in the WP header.php. I'm terrible with PHP, but this is what I have:
<?php
global $bp;
$the_user_id = $bp->loggedin_user->userdata->ID;
if (function_exists('bp_get_profile_field_data')) {
$bp_gamertag = bp_get_profile_field_data('field=Gamertag&user_id='.bp_loggedin_user_id());
if ($bp_gamertag) {
echo '<img src="http://avatar.xboxlive.com/avatar/$bp_gamertag/avatar-body.png" alt=""/>';
}
else
echo '<img src="http://avatar.xboxlive.com/avatar/xbox/avatar-body.png" alt=""/>';
}
?>
I can't quite figure out why it isn't working. The source shows the variable still in the URL.
Also, I don't think I need the $user_user_id variable, as it isn't really being used, do I? I'm following the instructions in this topic: http://buddypress.org/support/topic/how-to-get-user-profile-data/
Try :
echo "<img src='http://avatar.xboxlive.com/avatar/$bp_gamertag/avatar-body.png' alt='' />";
Notice the use of single vs. double quotes.

Create a search box that search between id of divs

Well I’m kind of new in this with web programing and I’m sure I’m breaking some rules but I really need a hand here. Well the idea it’s that I have created a slider with the information that I bring from the data base, so I’m working with php and I’m making that a piece of code in php fills all the li and divs for the slider, and I’m giving the id to the div that contains all the details the name of the user of the data base, so the deal it’s that I have to have a search field that looks through all the divs I have, and after the search it only display the name of the user you are looking in the search field.
So this is the code I have of php that’s in the page, well I try to use an ajax but when the information comes from the php file it doesn’t respect the size of the div and prints all the information and I don’t get the slider.
<div class="slider" id="slider">
<?php
$cont = 0;
$bandera=0;
$tamano = 0;
$nombreUsuario = "";
$arrayIDs = array();
$i = 0;
$query_Total = "SELECT u.FOTO_HIJO_USUARIOS, u.ID_USUARIOS, u.NOMBRE_USUARIOS, u.NOMBRE_HIJO_USUARIOS, u.FB_ID_USUARIOS, m.ID_MOMENTOS_MAGICOS, m.TEXTO_MOMENTOS_MAGICOS FROM MOMENTOS_MAGICOS AS m, USUARIOS AS u WHERE u.ID_USUARIOS = m.ID_USUARIOS AND u.ESTADO_USUARIOS = '1'";
$todas_Total = mysql_query($query_Total, $MM_db) or die(mysql_error());
echo "<ul>";
while(($row = mysql_fetch_array($todas_Total, MYSQL_ASSOC))){
$nombreUsuario = $row['NOMBRE_USUARIOS'];
$arrayIDs[$i] = $nombreUsuario;
$i++;
$bandera++;
if($cont++%3==0){
echo '<li>';
}
echo '<div class="participantes" id="'.$nombreUsuario.'" style="float:left; padding-left:10px;width:145px;margin-right:25px;margin-left:15px; margin-top:15px">';
echo '<div class="face_foto"><img src="https://graph.facebook.com/'.$row['FB_ID_USUARIOS'].'/picture" width="52" height="52" /></div>';
echo '<h1 align="right">'.$nombreUsuario.'</h1>';
echo '<img class="bebe_participante" src="uploads/'.$row['FOTO_HIJO_USUARIOS'].'" width="153" height="89" />';
echo '<h2 align="center">'.$row['NOMBRE_HIJO_USUARIOS'].'</h2>';
echo '<div id="botMas"> <img class="img_votos" src="images/bt_ver_mas.png" width="89" height="35"/> </div>';
$query_Votos = "SELECT COUNT( * ) AS VOTOS FROM VOTOS WHERE ID_MOMENTOS_MAGICOS = '".$row['ID_MOMENTOS_MAGICOS']."'";
$todos_Votos = mysql_query($query_Votos, $MM_db) or die(mysql_error());
while(($row = mysql_fetch_array($todos_Votos, MYSQL_ASSOC))){
echo '<div class="votos">'.$row['VOTOS'].' VOTOS </div>';
}
echo '</div>';
$tamano++;
}
if($cont%3==0 || $cont==$bandera ){
echo '</li>';
}
echo "</ul>";
?>
</div>
So I was thinking of making the search with javascript or jquery, but after some tries I haven’t been able to make it.
So I really hope you guys can help me.
Thank you for your time :)
Use http://ivaynberg.github.com/select2/ for this.
See: Many-to-Many Ajax Forms (Symfony2 Forms) for an implementation (ignore the fact that this is tagged symfony2, it is relevant to php as well)

How can I retrieve HTML/PHP code stored in a mySQL table and not have the PHP commented out?

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.

Categories