PHP Find value in array and echo something different - php

I have the following code:
$media = $db->query("SELECT * FROM uploaded_photos WHERE user_id='".$profile->id."' LIMIT 20");
$picheck = $db->query("SELECT * FROM users WHERE id='".$profile->id."'");
$picheck = $picheck->fetch_object();
if($media->num_rows == 0) {
$uploaded_photos = 0;
} else {
$uploaded_photos = array();
while($photo = $media->fetch_object()) {
$photos[] = array('type'=>'uploaded','id' => $photo->id, 'path' => $photo->path);
}
}
//IN A RELATED PHP FILE:
<?php
$c=0;
if(!empty($photos)) {
for($i = 0; $i < count($photos); $i++) {
if($photos[$i]['type'] == 'instagram') {
} else {
$c++;
echo '<div class="col-sm-3 col-md-3 col-lg-3 thumbnail m-5">';
echo '<div class="image-container">';
echo '<img src="'.$system->getDomain().'/uploads/'.$photos[$i]['path'].'" class="img-responsive">';
echo '
<div class="caption">
<h4>';
//HERE IS MY PROBLEM, IF THE PATH OF THE USERS CURRENT PROFILE PHOTO IS THE SAME AS THE PATH OF THE PHOTO DISPLAYED, IT SHOULD ECHO SOMETHING DIFFERENT.
if(in_array($picheck->profile_photo, $photos['path'], true)){ echo ' <i class="fa fa-exclamation" data-toggle="tooltip" data-placement="right" data-title="Your Profilepicture" placeholder="" data-original-title="" title=""></i> ';
}
else { echo ' <i class="fa fa-trash" data-toggle="tooltip" data-placement="right" data-title="'.$lang['Delete_Photo'].'" placeholder="" data-original-title="" title=""></i> ';
}
echo '
<i class="fa fa-user" data-toggle="tooltip" data-placement="left" data-title="'.$lang['Profile_Photo'].'" placeholder="" data-original-title="" title=""></i>
</h4>
</div>
';
echo '</div>';
echo '</div>';
}
}
}
if($c==0) {
echo 'YOU DONT HAVE ANY PICTURES';
}
?>
I have been searching high and low to solve this, I hope someone can help me out. I would like to thank all who put efford in helping me out on this beforehand.

The problem is because of this line,
if(in_array($picheck->profile_photo, $photos['path'], true)){ ...
^^^^^^^^^^^^^^^
The second argument of in_array() function i.e $photos['path'] is wrong because $photos array doesn't have an index named path.
So the correct condition for if block would be,
if(in_array($picheck->profile_photo, $photos[$i], true)){ ...

Related

Getting an input

I have to make a website that will rank project si I've decided to use star ranking system. All works well until I add a new project.
The problem is when I rate the second project my code think it is the first one that he need to rate.
When I get all the websites with PHP, the numbers are good (input type text to see the number)
What I see on the index
But when I click on the star below the number 51, the output of the echo is 53.
Gets all the websites :
try
{
$stmt = $db->query('SELECT sites.id, sites.programme, ROUND(AVG(rating.rating_number), 2) AS rating_number, sites.annee, sites.titre, sites.equipe, sites.image_preview, sites.lien, sites.campus, sites.date_send FROM sites
INNER JOIN rating
WHERE sites.id = rating.post_id
GROUP BY sites.id
ORDER BY ROUND(AVG(rating.rating_number), 2) DESC LIMIT 30');
while($row = $stmt->fetch())
{
$date_envoie_site = strtotime($row['date_send']);
$date_maintenant = strtotime("now");
$calcul = $date_maintenant - $date_envoie_site;
echo '<div class="col custom-card">';
echo '<div class="card shadow-6-strong h-100">';
echo '<img src="img/sites/' .$row["image_preview"]. '" class="card-img-top" height="320" alt=""/>';
echo '<div class="card-body">';
if ($calcul < 86400)
{
echo '<h4 class="card-title text-center"><span class="badge bg-primary">NOUVEAU</span> ' .$row["titre"]. '</h4> ';
}
else
{
echo '<h4 class="card-title text-center">' .$row["titre"]. '</h4> ';
}
echo '<hr>';
echo '<h6 class="card-title"><i class="fa-solid fa-city"></i> ' .$row["campus"]. '</h6>';
echo '<h6 class="card-title"><i class="fa-solid fa-book"></i> ' .$row["programme"]. '</h6>';
echo '<h6 class="card-title"><i class="fa-solid fa-calendar-alt"></i> ' .$row["annee"]. '</h6>';
echo '<h6 class="card-title"><i class="fa-solid fa-users"></i> ' .$row["equipe"]. '</h6>';
echo '<form method="POST">';
echo '<input type="text" name="post_id" value="'.$row['id'].'">';
echo '<div class="rating-wrap">
<div class="center">
<fieldset class="rating">
<input type="submit" id="star5" name="star5" value="5"/><label for="star5" class="full" title="Awesome"></label>
</fieldset>
</form>
</div>
<h4 id="rating-value"></h4>
</div>';
echo '<h6 class="card-title"><i class="fa-solid fa-star"></i> ' .$row["rating_number"]. '/5</h6>';
echo '</div>';
echo '<div class="card-footer text-center">';
echo '<button type="button" class="btn btn-primary btn-lg btn-block button-custom"><i class="fa-solid fa-eye"></i> Voir le site</button>';
echo '</div>';
echo '<div class="card-footer text-center">';
echo '<div class="d-flex justify-content-evenly pb-2">';
echo '<i class="fa-brands fa-facebook fa-3x"></i>';
echo '<i class="fa-brands fa-twitter fa-3x"></i>';
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
catch(PDOException $e) { echo $e->getMessage();}
When I click on the star
if (isset($_POST['star5']))
{
$id = $_POST['post_id'];
echo $_POST['post_id'];
/*
$sql = "INSERT INTO rating(post_id, rating_number) VALUES (:post_id, :rating_number) WHERE post_id = '$id'";
$stmt = $db->prepare($sql);
$stmt->bindParam(':post_id', $_POST['id'], PDO::PARAM_STR);
$stmt->bindParam(':rating_number', $_POST['star5'], PDO::PARAM_STR);
$stmt->execute();
$stmt->closeCursor();
*/
}

Select first 10 rows and display into a slideshow

I'm creating a website and I want to display my data into a slideshow from database. The slideshow is made using bootstrap.
My code looks like this:
<?php
include 'header.php';
require 'includes/dbh-inc.php';
function make_query($conn)
{
$query = " SELECT * FROM notes ORDER BY id ASC";
$result = mysqli_query($conn,$query);
return $result;
}
function make_slide_indicators($conn)
{
$output = '';
$count = 0;
$result = make_query($conn);
while($row = mysqli_fetch_array($result))
{
if($count == 0)
{
$output .= '<li data-target="#dynamic_slide_show" data-slide-to="'.$count.'" class="active"></li>
';
} else {
$output .= '<li data-target="#dynamic_slide_show" data-slide-to="'.$count.'"></li>';
}
$count = $count + 1 ;
}
return $output;
}
function make_slides($conn)
{
$output = '';
$count = 0;
$result = make_query($conn);
while($row = mysqli_fetch_array($result))
{
if($count == 0)
{
$output .= '<div class="item active">';
} else {
$output .= '<div class="item">';
}
$output .= '<img src="images/notes/'.$row["image"]
.'" alt="'.$row["denomination"].'" style=" width: 100%;height: 300px;" />
<div class="carousel-caption">
<h3>' .$row['price'].'</h3>
</div>';
$count = $count +1;
}
return $output;
}
?>
<main>
<div class="container">
<h2 align="center">Pedigree Notes</h2>
<br />
<div id="dynamic_slide_show" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php echo make_slide_indicators($conn); ?>
</ol>
<div class="carousel-inner">
<?php echo make_slides($conn); ?>
</div>
<a class="carousel-control-prev" href="#carouselExampleIndicators" role="button" data-slide="prev">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">Previous</span>
</a>
<a class="carousel-control-next" href="#carouselExampleIndicators" role="button" data-slide="next">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">Next</span>
</a>
</div>
</div>
</main>
<?php
include 'footer.php';
?>
It is working somehow, but it displays both pictures from database in first slide, half slide first pictures the other half the second picture.
Can you please tell me what I'm supposed to do?
In your function make_slides, change the class item to carousel-item
if($count == 0)
{
$output .= '<div class="carousel-item active">';
} else {
$output .= '<div class="carousel-item">';
}

Error in error log PHP Warning: Invalid argument supplied for foreach()

Error in error log but results are showing perfectly I don't know how and why this error is showing up in error file.
PHP Warning: Invalid argument supplied for foreach() please help me with this How can I get rid of this error?
<?php
$business_name = $value['business_name'];
$sql = "SELECT * FROM listings_reviews WHERE business_name = '$business_name' order by listing_date DESC";
$result = $conn -> query($sql);
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}
//print_r($row); //exit;
?>
<?php
foreach ($data as $value){ ?>
<div class="col-md-12 reviews">
<span style="color:#000"><?php echo hide_mail ($value['reviewer_email'])?></span>
<span style="color:#000">
<?php
$starNumber = $value['rating'];
//echo $starNumber; exit;
for($x=1;$x<=$starNumber;$x++) {
echo '<i style="font-size:12px; color:#00BF9A" class="fa fa-star" aria-hidden="true"></i>';
}
if (strpos($starNumber,'.')) {
echo '<i style="font-size:12px; color:#00BF9A" class="fa fa-star-half-o" aria-hidden="true"></i>';
$x++;
}
while ($x<=5) {
echo '<i style="font-size:12px; color:#00BF9A" class="fa fa-star-o" aria-hidden="true"></i>';
$x++;
}
?>
</span>
<span style="float:right; color:#666; font-size:14px"><?php echo $value['listing_date']?></span>
<hr style="margin:5px">
<span style="color:#666"><?php echo $value['comment']?></span>
</div>
<?php
}
?>
In some cases there are no results, so $data[] = $row; is never called and $data is undefined.
The fix is to properly initialize $data:
$data = []; // add this
while ($row = $result->fetch_assoc()) {
$data[] = $row;
}

PHP - Code only giving one value from Mysql database, while it should give more

I have a piece of code which executes a calendar. On this calendar certain names have to appear. However, on each date appears only one name, while in the database some dates hold up to 20 names.
This image should hopefully clarify things: http://imgur.com/a31pj0s
In this image you can see each date, which on some of them one name. As stated before, most of these contain many more names.
Now, I understand why this does not work, as it executes only once, but how can I get it to actually work? Please help me out :)
Here is also a link to the same piece of code, except easier to read: http://codepad.org/tVSzHDTx
<?php
$j = 2;
$i = 1;
//query the database
$query = mysql_query("SELECT * FROM months");
//fetch the results / convert results into an array
echo '<div class="accordion" id="calendar">';
while($rows = mysql_fetch_array($query)) {
$month = $rows['month'];
$id = $rows['id'];
echo '
<div class="accordion-group">
<div class="accordion-heading" id="month_'.$id.'">
<a class="accordion-toggle" data-toggle="collapse" data-parent="#calendar" href="#monthsid'.$id.'">
'.$month.'
</a>
</div>
<div id="monthsid'.$id.'" class="accordion-body collapse">
<div class="accordion-inner">';
$n = $rows['num_of_days'];
echo '<ul class="list-group" id="'.$id.'" style="margin-bottom: 0;">';
for ($i = 1; $i <= $n; $i++) {
if ($j == 7) {
echo '<li class="list-group-item" id="'.$i.'" style="margin-bottom: 5px;">';
} else {
echo '<li class="list-group-item" id="'.$i.'">';
}
echo '<a data-toggle="modal" href="#myModal" class="add_member" id="'.$month.' '.$i.'">+</a>
<span class="badge">'.$i.'</span>';
if ($i == date("j")) {
echo '<div class="day_segment current_day">';
} else {
echo '<div class="day_segment">';
}
if ($j == 1) {
echo '<i style="color: #aaa;">'.$i.' |</i> Monday';
$j++;
} else if ($j == 2) {
echo '<i style="color: #aaa;">'.$i.' |</i> Tuesday';
$j++;
} else if ($j == 3) {
echo '<i style="color: #aaa;">'.$i.' |</i> Wednesday';
$j++;
} else if ($j == 4) {
echo '<i style="color: #aaa;">'.$i.' |</i> Thursday';
$j++;
} else if ($j == 5) {
echo '<i style="color: #aaa;">'.$i.' |</i> Friday';
$j++;
} else if ($j == 6) {
echo '<i style="color: #aaa;">'.$i.' |</i> Saturday';
$j++;
} else if ($j == 7) {
echo '<i style="color: #aaa;">'.$i.' |</i> Sunday';
$j = 1;
}
echo '</div>';
$posts_query = mysql_query("SELECT * FROM posts WHERE day=$i ");
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
}
for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
echo '</li>';
}
echo '
</ul>
</div>
</div>
</div>
';
} /* End While Loop */
echo '</div></div>';
?>
As mentioned in the comments, you should not use the mysql_ functions as they are deprecated and not secure. Use MySQLi or PDO instead.
To answer your question, the code block below is the reason why it only displays one entry per day. In the while loop, you overwrite all the four variables each time, thus only the last one will be displayed in your for loop. The for loop is not necessary.
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
}
for ($k = 1; $k <= 1; $k++) { /* I tried using another for loop, did not work */
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}
Try changing your code to something like this:
while ($rows_items = mysql_fetch_array($posts_query)) {
$entry_player = $rows_items['author'];
$entry_comment = $rows_items['comment'];
$entry_day = $rows_items['day'];
$entry_month = $rows_items['month'];
if ($id == $entry_month && $i == $entry_day) {
echo '<span class="label label-success" data-toggle="tooltip" rel="tooltip" title="'.$entry_comment.'">'.$entry_player.'</span>';
} else {
echo '<span class="label"></span>';
}
}

php array and returning a default value?

Here is my code:
<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<?php
$currencies = explode(',', hana_get_opt('_cc'));
foreach ($currencies as $currency) {
if ($currency == get_option('woocommerce_currency')){
echo $currency;
break;
}else{
echo "Select Currency";
break;
}
}
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
<?php
foreach ($currencies as $currency) {
if ($currency == get_option('woocommerce_currency'))
echo '<li>' . $currency . '</li>';
else
echo '<li>' . $currency . '</li>';
}
?>
</ul>
</div>
</div>
Which works great it returns a list of my currencies and updates the page accordingly, I have just on question if the user has yet to set a value I would like it to say either select currency or just apply the first option from my hana_get_opt('_cc') array as the default.
Here is the html generated code:
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">undefined<span class="caret"></span></a>
<ul class="dropdown-menu currency_switcher">
<li>SEK</li>
<li>EUR</li>
</ul>
</div>
I am no php coder and much appreciate any help provided
Chris
if(get_option('woocommerce_currency')==' ')
{
$currencies = explode(',', hana_get_opt('_cc'));
foreach ($currencies as $currency)
{
if $currency == get_option('woocommerce_currency'){
{
echo $currency;
break;
}
else{
echo "Select Currency";
break;
}
}
}
else
{
echo "Select Currency";
$currencies = explode(',', hana_get_opt('_cc'));
$currency=$currencies [0];
}
based on the assumption that get_option('woocommerce_currency') would return null if no currency is selected
<div class="search-menu">
<div class="btn-group fc">
<a class="btn dropdown-toggle" data-toggle="dropdown" href="#">
<?php
$currencies = explode(',', hana_get_opt('_cc'));
$currentCurrency = get_option('woocommerce_currency')
if ($currentCurrency === false) {
echo "select currency";
}
else{
echo $currentCurrency;
}
?>
<span class="caret"></span>
</a>
<ul class="dropdown-menu currency_switcher">
<?php
foreach ($currencies as $currency) {
if ($currency == $currentCurrency)
echo '<li>' . $currency . '</li>';
else
echo '<li>' . $currency . '</li>';
}
?>
</ul>
replace the if clause if ( $currentCurrency != null ) with whatever applies, if get_option('woocommerce_currency') returns anything other as null if not yet set

Categories