Getting an input - php

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();
*/
}

Related

how do I store quantity from all items in session?

I'm not seeing what goes wrong here? I want to allow my users to up the quantity from an item in their shopping cart. when they press enter I want the quantity to change from 1 to the number the use rput in and I want that it calculates everything correctly. but right now it only wants to update the last item that the user changed the quantity from. how do I fix this? I thought of using a $_SESSION but that doesn't make any difference. this is part of the code
<body>
<!--navbar-->
<a class="back" href="index.php"> <i class="bi bi-arrow-left-circle-fill bi-5x"></i></a>
<?php
include "config.php";
?>
<div class="text-center" style="font-size: 100px;">🛍</div>
<h2 class="text-center">Winkelmandje</h2><br>
<section class="container content-section">
<!-- <h2 class="section-header">CART</h2> -->
<div class="cart-row">
<span class="cart-item cart-header cart-column">ITEM</span>
<span class="cart-item cart-header cart-column">PRICE</span>
<span class="cart-item cart-header cart-column">QUANTITY</span>
<span class="cart-item cart-header cart-column">berekening</span>
<!-- <span class="cart-item cart-header cart-column">Verwijderen</span> -->
</div>
<?php
$broodjes = $_GET['broodjes_ID'];
if (isset($_SESSION['basket'])){
if( in_array( $broodjes ,$_SESSION['basket']) )
{
}else{
$_SESSION['basket'][] = $broodjes;
}
}else{
$_SESSION['basket'][]= $broodjes;
}
$sumtotal = 0;
foreach($_SESSION['basket'] as $key => $value){
//echo "Key = $key; value = $value; <br>";
$sql = "SELECT broodjes_ID, broodnaam, prijs, voorraad FROM broodjes WHERE broodjes_ID=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $value);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
echo '<div class="cart-items">';
echo '<div class="cart-row">';
echo '<div class="cart-item cart-column">';
echo $row['broodnaam'];
echo '</div>';
echo '<div class="cart-item cart-column">';
echo '€ ' . $row['prijs'];
echo '</div>';
//quantity
echo '<div class="cart-item cart-column">';
echo '<form method="POST" action"">';
echo '<div class="col-xs-4">';
echo '<input type="hidden" name="broodnaam" id="broodnaam" value="' . $row['broodnaam'] . '">';
echo '<input type="number" name="quantity" id="quantity" class="form-control input-sm" placeholder="1" min="1" max="100" value="1">';
echo '</div>';
echo '</form>';
echo '</div>';
//session for quantity???'
$_SESSION['quantity'] = $_POST['quantity'];
$quantity = 1;
if (isset($_POST['quantity']) && !empty($_POST['quantity'])){
$_SESSION['quantity'] = $_POST['quantity'];
if (isset($_POST['broodnaam']) && !empty($_POST['broodnaam'])){
if ($_POST['broodnaam'] == $row['broodnaam']){
$quantity = $_POST['quantity'];
}
}
}
echo '<div class="cart-item cart-column">';
$rowtotaal = $row['prijs'] * $quantity;
$sumtotal += $rowtotaal;
echo $rowtotaal;
echo '</div>';
echo '</div>';
echo '</div>';
}
}
?> <br />
<div class="cart-total">
<strong class="cart-total-title">Total</strong>
<span class="cart-total-price"> € <?php echo $sumtotal;?></span>
</div>
<br/>
and this is what it does
now situation:
how do I store the information in a session??

Answering To Telegram Users Separately

I'm working on a Content Management System using PHP for controlling over my Telegram Bot. Basically what I have done till now is that I can read the messages that people has sent to my Telegram Bot and answer to them.
In order to do that, I coded this:
<?php
$botToken = '423495534:asdsadsadasdsadsa';
$website = 'https://api.telegram.org/bot'.$botToken;
$update = file_get_contents($website."/getUpdates");
$updateArray = json_decode($update, TRUE);
$info = file_get_contents($website."/getme");
$infoArray = json_decode($info, TRUE);
$num = count($updateArray["result"]);
$sender_ids = array();
$sender_infos = array();
for($i=0;$i<$num;$i++){
$sender_id = $updateArray["result"][$i]["message"]["from"]["id"];
$sender_isbot = $updateArray["result"][$i]["message"]["from"]["is_bot"];
$sender_fname = $updateArray["result"][$i]["message"]["from"]["first_name"];
$sender_lname = $updateArray["result"][$i]["message"]["from"]["last_name"];
$sender_uname = $updateArray["result"][$i]["message"]["from"]["username"];
$sender_type = $updateArray["result"][$i]["message"]["chat"]["type"];
$sender_msg = $updateArray["result"][$i]["message"]["text"];
$sender_date = $updateArray["result"][$i]["message"]["date"];
if(false === $key = array_search($sender_id, $sender_ids)){
$sender_ids[] = $sender_id;
$sender_infos[] = [
'sender_id' => $sender_id,
'sender_isbot' => $sender_isbot,
'sender_fname' => $sender_fname,
'sender_lname' => $sender_lname,
'sender_uname' => $sender_uname,
'sender_type' => $sender_type,
'sender_msg' => [$sender_msg],
'sender_date' => [$sender_date]
];
}else{
$sender_infos[$key]['sender_msg'][] = $sender_msg;
$sender_infos[$key]['sender_date'][] = $sender_date;
}
}
$num2 = count($sender_ids);
for($j=0;$j<$num2;$j++){
$id = $sender_infos[$j]["sender_id"];
$first_name = $sender_infos[$j]["sender_fname"];
$last_name = $sender_infos[$j]["sender_lname"];
$username = $sender_infos[$j]["sender_uname"];
$messages = $sender_infos[$j]["sender_msg"];
$acc_type = $sender_infos[$j]["sender_type"];
$isbot = $sender_infos[$j]["sender_isbot"];
$num1 = count($messages);
echo '
<div class="col-md-3">
<div class="box box-danger direct-chat direct-chat-danger">
<div class="box-header with-border">
<h3 class="box-title">'.$first_name.'</h3>
<sup>'.$acc_type.' - '.$isbot.'</sup>
<div class="box-tools pull-right">
<span data-toggle="tooltip" title="'.$num1.' New Messages" class="badge bg-red">'.$num1.'</span>
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-toggle="tooltip" title="Contacts" data-widget="chat-pane-toggle">
<i class="fa fa-comments"></i></button>
<button type="button" class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
</div>
</div>
<div class="box-body">
<div class="direct-chat-messages">
<div class="direct-chat-msg">
<div class="direct-chat-info clearfix">
<span class="direct-chat-name pull-left">';
echo '<strong>'.$first_name.'</strong>';
echo '
</span>
</div>';
for($i=0;$i<$num1;$i++){
echo '<div class="direct-chat-text">';
$text = $sender_infos[$j]["sender_msg"][$i];
if($text[0] === '/') {
echo ''.$text.'';
}else{
echo $text;
}
echo '<span class="direct-chat-timestamp pull-right">';
// echo date('l', $sender_infos[$j]["sender_date"][$i]);
echo '</span>';
echo '</div>';
}
echo '
</div>
<div class="direct-chat-msg right">
<div class="direct-chat-info clearfix">
<span class="direct-chat-name pull-right">'.$bot_fname.'</span>
</div>
<img class="direct-chat-img" src="
';
if ($dataSet->GetAvatar() != NULL){
echo $dataSet->GetAvatar();
}else{
echo "img/noprofilepic.jpg";
}
echo '
" alt="Message User Image">
';
$num3 = count($request_params["text"]);
foreach($_SESSION['messages'] as $reply){
echo '<div class="direct-chat-text">';
echo $reply;
echo '</div>';
}
echo '
</div>
</div>
<div class="direct-chat-contacts">
<ul class="contacts-list">
<li>
<a href="#">
<div class="contacts-list-info">
<span class="contacts-list-name">
';
echo $first_name = $sender_infos[$j]["sender_fname"];
echo ' </br> ';
echo $last_name = $sender_infos[$j]["sender_lname"];
echo ' </br> ';
echo ' <a target="_blank" href="http://www.t.me/'.$username.'">'.$username.'</a>';
echo ' </br> ';
echo ' <small class="contacts-list-date pull-right">'.$id.'</small>
</span>
<span class="contacts-list-msg">QUOTE</span>
</div>
</a>
</li>
</ul>
</div>
</div>';
if (isset($_POST['send'])){
$pm = $_POST['message'];
array_push($_SESSION['messages'], $pm);
$request_params = [
'chat_id' => $id,
'text' => $pm
];
echo "<META HTTP-EQUIV='Refresh' Content='0; URL=telegrambots.php?user_name=".$user_name."'>";
$request_url = 'https://api.telegram.org/bot' . $botToken . '/sendMessage?' . http_build_query($request_params);
$response = file_get_contents($request_url);
}
echo '
<div class="box-footer">
<form action="" method="post">
<div class="input-group">
<input type="text" name="message" placeholder="Write your direct message" class="form-control">
<span class="input-group-btn">
<input name="send" type="submit" class="btn btn-danger btn-flat"/>
</span>
</div>
</form>
</div>
</div>
</div>
';
}
?>
Basically what it does is that it grabs the latest updates of my Bot and count the number of results and save it in $num. Then with a for loop I tried to divide every information about the user who has sent message. After that I store all the required information of sender in a separated array called $sender_infos. And the next for loop shows a basic Chat Box depending on the number of users.
This whole thing makes this:
So it works fine and perfect but the problem is that, whenever I try answering to one conversation, it sends the message to all the available users. This issue comes from the for loop which divides every users by the Chat Box.
However what I want to do is to send a SINGLE DIRECT MESSAGE to a CUSTOM user and NOT ALL OF THEM.
I hope I have explained my problem well, so you could understand. If not, please comment me for more information and additional updates.
NOTE: I don't want a quick answer to this question. Because I'm facing this for several days and I don't know how to solve it. Please make sure you understand what I'm asking for and then add your suggest.
Thanks in advance...

PHP Find value in array and echo something different

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)){ ...

Search Button in php No Result error message

I'm having trouble where should i put my echo 'No Result Found'; in the code code I tried putting it in the last else statement together in code for debugging but it doesn't work. Please help where should I put my no result found. Thanks in advance.
if(!empty($_POST['search'])){
if($result = $db->query("SELECT * FROM product WHERE setname like '%".$_POST['search']."%' OR category like '%".$_POST['search']."%' ")) {
while($row = $result->fetch_assoc()) {
echo '<div class="col-sm-3">';
echo '<form method="POST" action="buynow.php" enctype="multipart/form-data"> ';
echo '<input type="hidden" name="productid" value="'.$row['id'].'">';
echo '<img class="thumbnail img-responsive" src="data:image;base64,'.$row['image'].' " >';
echo '<p>Name : ',$row['setname'], '</span></p>';
echo '<p>Price : ', $row['price'], '</span></p>';
echo '<p>Bonus : <span class="label label-info" style="font-size:16px;">', $row['status'], '</span></p>';
echo '<p>Price Now : ', $row['pricesale'], '</span></p>';
echo '<p>Product Detail: ', $row['productdesc'] ,'</p>';
echo '<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>';
echo '</form>';
echo '<br></div>';
}
echo '</div>';
echo '</div>';
}else{
//code for debugging query
die($db->error);
}
}
You need to count number of num_rows from your query. If no rows found the show no result found
if (!empty($_POST['search'])) {
if ($result = $db->query("SELECT * FROM product WHERE setname like '%" . $_POST['search'] . "%' OR category like '%" . $_POST['search'] . "%' ")) {
$row = $result->num_rows;
if ($row > 0) {
while ($row = $result->fetch_assoc()) {
echo '<div class="col-sm-3">';
echo '<form method="POST" action="buynow.php" enctype="multipart/form-data"> ';
echo '<input type="hidden" name="productid" value="' . $row['id'] . '">';
echo '<img class="thumbnail img-responsive" src="data:image;base64,' . $row['image'] . ' " >';
echo '<p>Name : ', $row['setname'], '</span></p>';
echo '<p>Price : ', $row['price'], '</span></p>';
echo '<p>Bonus : <span class="label label-info" style="font-size:16px;">', $row['status'], '</span></p>';
echo '<p>Price Now : ', $row['pricesale'], '</span></p>';
echo '<p>Product Detail: ', $row['productdesc'], '</p>';
echo '<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>';
echo '</form>';
echo '<br></div>';
}
} else {
echo "No result Forund";
}
} else {
//code for debugging query
die($db->error);
}
}
echo '</div>';
echo '</div>';
You can either use $result->num_rows or mysqli_num_rows($result)
if ($result->num_rows == 0) {
echo 'No Result Found';
}
else {
// Do your while
}
And please use prepared statements
Use prepared statements.
Use PDO, not mysqli.
Use templates.
So it goes
$sql = "SELECT * FROM product WHERE setname like :search OR category like :search";
$stmt = $pdo->prepare($sql);
$stmt->execute(array('search' => '%'.$_POST['search'].'%'));
$data = $stmt->fetchAll();
?>
<? foreach($data as $row): ?>
<div class="col-sm-3">
<form method="POST" action="buynow.php" enctype="multipart/form-data">
<input type="hidden" name="productid" value="<?=$row['id']?>">
<img class="thumbnail img-responsive" src="data:image;base64,<=$row['image']?>">
<p>Name : <?=$row['setname']?></span></p>
<p>Price : <?=$row['price']?></span></p>
<p>Bonus : <span class="label label-info" style="font-size:16px;">
<?=$row['status']?>
</span></p>
<p>Price Now : <?=$row['pricesale']?></span></p>
<p>Product Detail: <?=$row['productdesc']?></p>
<button class="btn btn-danger btn-lg btn-block" type="submit" name="submit">Buy Now</button>
</form>
<br>
</div>
<? endforeach ?>
<? if (!$data): ?>
No Result Found
<? endif ?>

How to use two variable in one foreach?

Im want to generate the Boostrap Acordion with 2 diferent taxonomies (var). But Im not able to Combine both in one Foreach. Also I look in this Forum and I try some of the anwers but I just get errors.
<?php
$temporada = 'temporada';
$capitulo = 'capitulo';
$tax_temporada = get_terms($temporada);
$tax_capitulo = get_terms($capitulo);
foreach ($tax_temporada as $tax_temporada) {
echo '<div class="panel panel-default">';
echo '<div class="panel-heading" role="tab" id="c'. $tax_capitulo->name.'">';
echo '<h4 class="panel-title">' . '<a data-toggle="collapse" data-parent="#accordion" href="#'. $tax_temporada->name.'" aria-expanded="true" aria-controls="' . $tax_temporada->name.'" >Temporada: ' . $tax_temporada->name.'</a></h4>';
echo '</div>';
echo '<div id="'. $tax_temporada->name.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="c'. $tax_capitulo->name.'">';
echo '<div class="panel-body">';
echo 'Capitulo '. $tax_capitulo->name.' ';
echo '</div>'.'</div>'.'</div>';
}
?>
The Problem Is I just can get One Var And not Both
This One here fix the Problem but The thing is Im not able to more than 1 $capitulo inside of each $temporada
<?php
$temporada = 'temporada';
$capitulo = 'capitulo';
$tax_temporada = get_terms($temporada);
$tax_capitulo = get_terms($capitulo);
for ($i = 0; $i < count($tax_temporada); $i++) {
echo '<div class="panel panel-default">';
echo '<div class="panel-heading" role="tab" id="c'. $tax_capitulo[$i]->name.'">';
echo '<h4 class="panel-title">' . '<a data-toggle="collapse" data-parent="#accordion" href="#'. $tax_temporada[$i]->name.'" aria-expanded="true" aria-controls="' . $tax_temporada[$i]->name.'" >Temporada: ' . $tax_temporada[$i]->name.'</a></h4>';
echo '</div>';
//Here Start the $capitulos that go inside of every $temporadas
echo '<div id="'. $tax_temporada[$i]->name.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="c'. $tax_capitulo[$i]->name.'">';
echo '<div class="panel-body">';
echo 'Capitulo '. $tax_capitulo[$i]->name.' ';
echo '</div>'.'</div>'.'</div>';
}
?>
Are both of these going to be arrays of objects, of the same length? If so, you could consider doing a simple for loop, as such:
<?php
$temporada = 'temporada';
$capitulo = 'capitulo';
$tax_temporada = get_terms($temporada);
$tax_capitulo = get_terms($capitulo);
for ($i = 0; $i < count($tax_temporada); $i++) {
echo '<div class="panel panel-default">';
echo '<div class="panel-heading" role="tab" id="c'. $tax_capitulo[$i]->name.'">';
echo '<h4 class="panel-title">' . '<a data-toggle="collapse" data-parent="#accordion" href="#'. $tax_temporada[$i]->name.'" aria-expanded="true" aria-controls="' . $tax_temporada[$i]->name.'" >Temporada: ' . $tax_temporada[$i]->name.'</a></h4>';
echo '</div>';
echo '<div id="'. $tax_temporada[$i]->name.'" class="panel-collapse collapse" role="tabpanel" aria-labelledby="c'. $tax_capitulo[$i]->name.'">';
echo '<div class="panel-body">';
echo 'Capitulo '. $tax_capitulo[$i]->name.' ';
echo '</div>'.'</div>'.'</div>';
}
?>
The MultipleIterator allows you to loop over several arrays at the same time
$tax_temporada = get_terms($temporada);
$tax_capitulo = get_terms($capitulo);
$taxIterator = new MultipleIterator();
$taxIterator->attachIterator(new ArrayIterator($tax_temporada));
$taxIterator->attachIterator(new ArrayIterator($tax_capitulo));
foreach($taxIterator as list($temporada, $capitulo)) {
... do what you want here with $temporada and $capitulo
}
EDIT
Note that unpacking nested arrays with list() requires PHP >= 5.5.0

Categories