How to concatenate sql, html and php with this code - php

Basically it is an element which shows an image from the path on database. But I just can't make it work.
<img class="img-circle profile_img" id="blah" src=" <?php $query2=mysqli_query($conexao,"select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
while($row2=mysqli_fetch_array($query2)){
if ((!empty($row2['img_local'])) && (file_exists($row2['img_local']))) {
echo '<img class="image--cover" id="blah" src="'.$row2['img_local'].'" alt="Avatar" title="DEFINIDA" onerror="this.onerror=null;this.src=1.png;">';
} else {
echo 'Show a default image.'; // <img src="path_to_default_image" alt="Default_image"/>
}
} ?>" alt="Avatar" title="DEFINIDA" >

I have managed to work, the final code:
<?php
$query2=mysqli_query($conexao,"select * FROM esc_usuarios_fotos WHERE img_usu_codigo = '" . $_SESSION['codigo'] . "'");
while($row2=mysqli_fetch_array($query2)){
if ((!empty($row2['img_local'])) && (file_exists($row2['img_local']))) {
echo '<img class="image--profile" src="'.$row2['img_local'].'" title="Clique para abrir seu perfil">';
} else {
echo '<img class="image--profile" src="images/user.png">';
}
}
?>

Related

how to mysqli num rows simple echo and empty database

The text in echo does not show. Why isn't it displayed?
<?php
while ($liste1 = mysqli_fetch_assoc($resultat8)){
if(isset($liste1['naissancesID'])){
echo '<div class="photos" align="center"><img alt="" src="changements/naissances/images/' . $liste1["naissancesPic"] . '" class="deces">
<span class="caption">' . $liste1["naissancesName"] . '<p class="texte-deces">' . $liste1["naissancesDate"] . '</p></span></div>';
}else{
echo "No value in database";
}
}
?>
If no rows are returned, the while loop will end immediately, so you'll never get to the if statement. And when you fetch a row successfully, the ID field will always be filled in, so isset() will never return FALSE.
You should test the number of rows before the loop.
if (mysqli_num_rows($resultat8) > 0) {
while ($liste1 = mysqli_fetch_assoc($resultat8)){
if(isset($liste1['naissancesID'])){
echo '<div class="photos" align="center"><img alt="" src="changements/naissances/images/' . $liste1["naissancesPic"] . '" class="deces">
<span class="caption">' . $liste1["naissancesName"] . '<p class="texte-deces">' . $liste1["naissancesDate"] . '</p></span></div>';
}
}
} else {
echo "No value in database";
}

How to display name under photo in a foreach loop

Edit: I'm using php to accomplish this.
I'm making a page that draws information from a database (name, photo path) and I want it to show the picture and then the name of who it is underneath.
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/> '.$fullname.' ';
}
As of now, the name is displayed next to the photo and just looks bad. How would I go about making the name appear below the photo?
While a little more code than simply adding a line break, you could wrap your image in a <figure> and add a <figcaption> after your <img>.
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<figure><img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><figcaption>'.$fullname.'</figcaption></figure>';
}
To display multiple images per line, add the following to your CSS:
figure {
display: inline-block;
}
Check it out in this jsfiddle.
Try this: Add <br> to create a line break
foreach ($result_array as $la ) {
$firstname = $la['first_name'];
$lastname = $la['last_name'];
$fullname = $firstname . ' ' . $lastname;
$image = $la['photo'];
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/> <br>'.$fullname.' ';
}
There are different options for this depending on your needs. A simple solution is to add a line break after the image:
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><br /'.$fullname.' ';
If you want more control on styling, you can wrap the name in a div or span and assign a class to style it further with CSS:
echo '<img src="' . $image . '" width="100px" height="100px" alt="'.$fullname.'"/><div class="imagename">'.$fullname.'</div>';
Of course you could also wrap the entire image and text in a div, with or without the full name in its own div within there for further styling flexibility.

Multiple page ids in an array

I'm fairly new to PHP so go easy.
<?php
$body_classes = get_body_class();
$ScanL="url";
if(in_array('parent-pageid-385', $body_classes))
{
echo '<img src="'. $ScanL.'/images/product-logos/image1.gif" />';
}
else if(in_array('parent-pageid-394', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image2.gif" />';
}
else if(in_array('parent-pageid-238', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image3.gif" />';
}
else if(in_array('parent-pageid-382', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image4.gif" />';
}
else if(in_array('page-id-391', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image5.gif" />';
}
else if (in_array('page-id-334', $body_classes)) {
echo '<img src="'. $ScanL.'/images/product-logos/image6.gif" />';
}
if(in_array('parent-pageid-385', $body_classes))
I need the above to be applied to more than one page id - I tried adding , '' but that broke the code and also tried or / ||
I'm sure I'm probably missing something obvious.
Thanks,

How to add html tags in a php

I'm new to php. Does not know how to use with expressions.
Here is a php code from search file of my site.
I want add html tags such as href, div, li, ul, etc. How could i do it?
if(mysqli_num_rows($results) >= 1)
{
$output = "";
while($row = mysqli_fetch_array($results))
{
$output .= "Size: " . $row['fh_Vsize']. "<br />";
$output .= "Icon: <img src=" . $path .$row['fh_Sicon']. " alt=><br />";
$output .= "File ID: " . str_replace("_"," ",$row['fh_Sid']) . ""; $nbsp; $nbsp; $output .= "" . $row['fh_Vcaption'] . "<br />";
$output .= "Description: " . substr(strip_tags($row['fh_Sdescription']),0,150) . "<br />";
$output .= "Download: <a href=download_" . $row['fh_Sid'] . "/>Download</a><br /><br />";
}
echo $output;
}
else
echo "There was no matching record for the name " . $searchlink;
Something like this. I'm sorry for this code. I'm new here and will learn the basics:
<div class="info">
<h4>'.de_string($rec['fh_Sid']) .' ' .$rec['fh_Vcaption'] .' '. $rec['fh_Vextension']. '<span class="updated">updated</span></h4>
<div><img style="float:left;margin-right:5px" src="'. $path . str_replace("t_", "",$rec['fh_Sicon'] ).'"/></div><p>'. substr (strip_tags($rec['fh_Sdescription']),0,170) .' ...</p>
<ul class="prod-info">
<li class="none-separator">Last update: <strong>' . strftime("%d %b %Y", $rec['fh_Vdate']) . '</strong></li>
<li>License: '. $rec['fh_Vlicense'] . '</li>
<li>Size: <strong>'.$rec['fh_Vsize'].'</strong></li>
<li>Downloads: <strong>'. $views[$sid] .'</strong></li>
<li><img src="../../images/dl.png"/> Download</li>
</ul>
</div>
I am sorry this is not a full code
but basically you can use something like this
while ($line1 = mysqli_fetch_object($result2))
{
$i=0;
$id = $line1->$index; // fetching the whole object id of the SQL table
foreach ($line1 as $value1)
{
list($idName,$ext) = explode(".",$value1);
if ($i==1) // to show only the icon name
{
echo '
<li id='.$value1.' name="'.$id.'">
<form id="addchart'.$idName.'" action="addchart.php" target = "_self" method="post">
<a id='.$idName.' onmousedown = "AddorDelete(this.id,this.value)" href = "#" value='.$idName.'>
<img id="picture" class="icon" src="images/item/php.png" width="70" style="margin: 0px 0px 0px 0px"/>
<input type="hidden" name="db" value="'.$db.'" />
<input type="hidden" name="table" value="'.$table.'" />
<input type="hidden" name="id" value="'.$id.'" />
You could just echo 'html code and the attribute here'
be careful of the syntax to assign value, name or id if you want to assign the value, name or id with the predefined PHP variable you have made.
I prefer this way, which will let you have both php and html in their own format and easy to update:
<?php
if(mysqli_num_rows($results) >= 1)
{
while($row = mysqli_fetch_array($results))
{
?>
<div class="info">
<h4><?=de_string($rec['fh_Sid']) .' ' .$rec['fh_Vcaption'] .' '. $rec['fh_Vextension']?><span class="updated">updated</span></h4>
<div><img style="float:left;margin-right:5px" src="<?=$path . str_replace("t_", "",$rec['fh_Sicon'] )?>"/></div><p><?=substr (strip_tags($rec['fh_Sdescription']),0,170)?> ...</p>
<ul class="prod-info">
<li class="none-separator">Last update: <strong><?=strftime("%d %b %Y", $rec['fh_Vdate'])?></strong></li>
<li>License: <?=$rec['fh_Vlicense']?></li>
<li>Size: <strong><?=$rec['fh_Vsize']?></strong></li>
<li>Downloads: <strong><?=$views[$sid]?></strong></li>
<li><img src="../../images/dl.png"/> Download</li>
</ul>
</div>
<?php
}
}
else
echo "There was no matching record for the name " . $searchlink;
?>

Default avatar pic always showing in Pal requests

im trying to develop a social networking application and im having some trouble showing a user's selected avatar. Every users can upload pictures and there is a field called avatar and when this is set to 1, it is the users avatar pic. Only one pic can be an avatar.
My PHP code:
<div class="interactContainers" id="pal_requests">
<?php
$pal_requests = "SELECT * FROM pal_requests WHERE mem2='$user_id' ORDER BY pal_request_id ASC LIMIT 50";
$pal_request_query = mysql_query($pal_requests) or die(mysql_error());
$pal_request_num_rows = mysql_num_rows($pal_request_query);
if($pal_request_num_rows < 1)
{
echo ' You have no Pal requests at this time.';
exit();
}
else
{
while($row = mysql_fetch_array($pal_request_query))
{
$request_id = $row["pal_request_id"];
$mem1 = $row["mem1"];
$query_user = "SELECT user_first_name, user_last_name, picture_thumb_url FROM users LEFT JOIN picture ON users.user_id = picture.user_id
AND picture.avatar=1 WHERE users.user_id='$mem1' LIMIT 1";
$user_info = mysql_query($query_user, $connections) or die(mysql_error());
}
if(!empty($row["picture_thumb_url"]))
{
$avatar = '<img src="/NNL/User_Images/$row["picture_thumb_url"]" width="50" height="50" border="0"/>';
}
else
{
$avatar = '<img src="/NNL/Style/Images/default_avatar.png" width="50" height="50" border="0"/>';
}
}
?>
<?php
while ($row = mysql_fetch_array($user_info)){ $requesterFirstName = $row["user_first_name"]; $requesterLastName = $row["user_last_name"]; }
{
$thumbnail_user = $row["picture_thumb_url"] != '' ? $row["picture_thumb_url"] : '../Style/Images/default_avatar.png';
echo '<hr />
<table width="100%" cellpadding="5">
<tr>
<td width="17%" align="left"><div style="overflow:hidden; height:50px;">'. $avatar .'</div></td>
<td width="83%"><a class="ordinary_text_12_blue "href="user_view.php?user_id2=' . $mem1 . '">'. $requesterFirstName .' '. $requesterLastName .'</a> wants to be your Pal<br /><br />
<span id="req' . $request_id . '">
<a class="ordinary_text_12" href="#" onclick="return false" onmousedown="javascript:acceptFriendRequest(' . $request_id . ');" >Accept</a>
OR
<a class="ordinary_text_12" href="#" onclick="return false" onmousedown="javascript:denyFriendRequest(' . $request_id . ');" >Deny</a>
</span></td>
</tr>
</table>';
}
?>
</div>
The default avatar is being called all the time even though a user has selected their own avatar. Can someone tel me what im doing wrong?
This Block of code should be inside the body of the second while loop. In first while loop the variable $row["picture_thumb_url"] is never set.
if(!empty($row["picture_thumb_url"]))
{
$avatar = '<img src="/NNL/User_Images/$row["picture_thumb_url"]" width="50" height="50" border="0"/>';
}
else
{
$avatar = '<img src="/NNL/Style/Images/default_avatar.png" width="50" height="50" border="0"/>';
}

Categories