PHP MYSQL "more info" button - php

I have a question:
In PHP, HTML and MySQL I have made an overview of products (data from database)
Now each product in this list has a more info button. I want, when I click more info, that a new page is opened and that page should show more info about that product.
How do I make it work that it only shows the info of that particular product?
This is the current code of the table with the overview:
<div class="col-xs-2">
</div>
<div class="col-xs-4">
<h4 class="product-name"><strong> Producten</strong></h4><h4><small></small></h4>
</div>
<div class="col-xs-6">
<div class="col-xs-6 text-right">
<h6><strong>Datum binnenkomst</strong></h6>
</div>
<div class="col-xs-4">
<h6><strong>Datum uitgave</strong></h6>
</div>
<div class="col-xs-2">
</div>
</div>
</div>
<hr>
<?php
include("connection.php");
$sql = "SELECT * FROM bedrijven";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo'
<div class="row">
<div class="col-xs-2"><img class="img-responsive" style="max-height:80px;" src="' . $row["KVK"] . '">
</div>
<div class="col-xs-4">
<h4 class="product-name"><strong> '. $row["Bedrijfsnaam"] .' </strong></h4><h4><small>'. $row["Klantnummer"] .'</small></h4>
</div>
<div class="col-xs-6">
<div class="col-xs-6 text-right">
<h6><strong>'. $row["BStraatnaam"] .'</strong></h6>
</div>
<div class="col-xs-4">
<h6><strong>'. $row["BHuisnummer"] .'</strong></h6>
</div>
<div class="col-xs-2">
<a href="Moreinfo.php"> <button type="button" class="btn btn-link btn-xs">
More info
</button></a>
</div>
</div>
</div>
<hr>';
}
} else {
echo "0 results";
}
$conn->close();?>
I hope someone would help me.
It would be nice to learn more and new things;)

If you want to split up results only print out what you want to show. You can do selective queries with LIMIT (for a range) or just make sure you only get one result usually done by using a unique identifier (an auto-incremented ID field) and WHERE id = 1.
And in PHP we can use $_GET['page'], to get the page parameter from an url like:
domain.com/somepage?page=2
However remember that this is easily edited by the end user in the location bar and thus should be properly handled in PHP before using it in a SQL query to avoid SQL injection. It be checking with PHP's is_number or using something like prepared statements

Related

Jquery Ajax loop request problem with attribute

I want to get the name of two contestants from the database using PHP loop and at the same time calculate the total vote of each candidate and display it using ajax jquery but am only getting the first candidate votes while the other candidate is not even showing anything, not even 0.
index.php
<h4 class="header-title mb-3">Presidential Result</h4>
<div class="">
<div class="row text-center">
<?php
$sql = "SELECT * FROM contestants WHERE post='president'";
$query = mysqli_query($conn, $sql);
while($row=mysqli_fetch_array($query)){
?>
<div class="col-md-6">
<p class="text-muted mb-0 mt-3" id="contestname"><?php echo $row['fullname']?></p>
<h2 class="fw-normal mb-3" id="precount">
</h2>
</div>
<?php
}
?>
</div>
The index.php only displays the name of the contestants/candidates.
Query.js
function view_pres_count(){
var presname =$('#contestname').html();
$.ajax({
url: 'prescount.php',
method: 'post',
data:{present:presname},
success: function(data){
data = $.parseJSON(data);
$('#precount').html(data.html);
}
})
}
in Query.js I passed each name to presname using the tag id. At this point even without the ajax request, if I log presname to console I only got the first candidate name.
Any way out of this?
IDs need to be unique. Change to class and do
const presnames = $('.contestname').map(function() {
return this.textContent.trim()
}).get()
console.log(presnames)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<h4 class="header-title mb-3">Presidential Result</h4>
<div class="">
<div class="row text-center">
<div class="col-md-6">
<p class="text-muted mb-0 mt-3 contestname">
Jimmy Carter
</p>
<h2 class="fw-normal mb-3" class="precount">
</h2>
</div>
<div class="col-md-6">
<p class="text-muted mb-0 mt-3 contestname">
Ulyssus Grant
</p>
<h2 class="fw-normal mb-3" class="precount">
</h2>
</div>
</div>
You will need to give more details of your precount html but I suggest you return an array and loop over
$(".precount").each(function(i) { $(this).html(data[i].html) })

How to style different futured post in seperate row using bootstrap and php?

I am trying to create categories nav-pill with bootstrap 5.
İt has two cols first one is futured post and second one displays 5 posts.
I have tried like this but it wasn't right because they are in different columns (side by side), simple example:
$sql = "SELECT * FROM articles WHERE article_id = ? ORDER BY article_id DESC";
$result = $pdo->prepare($sql);
$result->execute([$cats['cat_id']]);
if ($result->rowCount() > 0) {
$array = array('class 1', 'class 2');
$i=0;
while($row = $result->fetch()) { ?>
<div class="<?php $array[$i];?>">
</div>
<?php
$i++;
}
}
?>
My html:
<div class="row">
<!-- first futured column ->
<div class="col-sm-6">
<div class="whats-news-single mb-40 mb-40">
<div class="whates-img">
<img src="..." alt="">
</div>
<div class="whates-caption">
Secretart for Economic Air plane that looks like
<p>Struggling to sell one the market won’t stop actress and the and singer Jennifer Lopez.</p>
<span class="small">by Alice cloe - Jun 19, 2020</span>
</div>
</div>
</div>
<!-- second column displays 5 articles ->
<div class="col-sm-6">
<div class="card mb-3">
<div class="row g-3">
<div class="col-4">
<img class="rounded" src="..." alt="">
</div>
<div class="col-8">
<h6>The pros and cons of business agency</h6>
<div class="small mt-1">May 17, 2021</div>
</div>
</div>
</div>
<div class="card mb-3">
<div class="row g-3">
<div class="col-4">
<img class="rounded" src="..." alt="">
</div>
<div class="col-8">
<h6>5 reasons why you shouldn't startup</h6>
<div class="small mt-1">Apr 04, 2021</div>
</div>
</div>
</div>
<div class="card mb-3">
<div class="row g-3">
<div class="col-4">
<img class="rounded" src="..." alt="">
</div>
<div class="col-8">
<h6>Ten questions you should answer truthfully.</h6>
<div class="small mt-1">Jun 30, 2021</div>
</div>
</div>
</div>
<div class="card mb-3">
<div class="row g-3">
<div class="col-4">
<img class="rounded" src="..." alt="">
</div>
<div class="col-8">
<h6>Five unbelievable facts about money.</h6>
<div class="small mt-1">Nov 29, 2021</div>
</div>
</div>
</div>
</div>
</div>
This is how it has to look like
enter image description here
I'm a bit confused by your question. Is the featured Post a randomly selected post, or is it always the newest in the database? If its always the newest, you could just query by article_id desc (as you're currently doing) and then access the first post via:
$array[0]
That way you can now create your featured post view by using all the data available in the first index of your array.This only works if you put your query results into an array, as described here:. Note that you should use mysqli functions, and not mysql. For the next five, you can then make use of for to display five more articles:
for($i = 1; $i <= 5;$i++) {
// repeat the section for one post five times
}
This will allow you to create the view you described.

Dynamically form columns in bootsrap using php

i want to dynamically display content in 3 bootstrap colums.
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
<content>
</div> <---- Problem (should be placed at the very end of the column,
but the same code should also fill 2nd and 3rd columns)
while i fetch the query to mysql. Also i want object to sort in descending order from left to right (id = 51, id = 50, id = 42 (last digit in the object title is id))
I was able to do that, using row_number and row_number%3=0 row_number%3=1 and row_number%3=2 to display 3 columns, but i have to make 3 queries to the database along with 3 blocks of html(which is bad).
SELECT *
FROM (
SELECT
#row_number:=#row_number + 1 AS rownum, id,title, description, ...etc
FROM (
SELECT #row_number:=0) r, flats ORDER BY id DESC) ranked WHERE rownum%3=1 ORDER BY id DESC
But, when i started to implement filters, it caused problems with displaying objects, because not every column had the object i wanted to display, but i had own query for every column.
Problems with filters
What i want: Dynamically form 3 separate columns with content to use one MYSQL query.
Waiting for your help!
Example of 1 of 3 columns:
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4">
<?php
$zapros = $DB_con->prepare('SELECT *
FROM (
SELECT
#row_number:=#row_number + 1 AS rownum, id,title, description,type, area,address, square, name, phone, email, price, avito, farpost
FROM (
SELECT #row_number:=0) r, flats ORDER BY id DESC) ranked WHERE rownum%3=1 ORDER BY id DESC');
$zapros->execute();
$rowsone=$zapros->rowCount();
if($zapros->rowCount() > 0)
{$tmpCount = 1;
$count=$zapros->rowCount();
while($data=$zapros->fetch(PDO::FETCH_ASSOC))
{
extract($data);
$number=$data['id'];
$imagestm=$DB_con->prepare('SELECT file FROM flats_images WHERE post_id=:uid LIMIT 1');
$imagestm->bindParam(':uid',$number);
$imagestm->execute();
$imgrow=$imagestm->fetch(PDO::FETCH_ASSOC);
extract($imgrow);?>
<div class="well-lg well-smallborders">
<a href="flat.php?flat_id=<?php echo $data['id']; ?>">
<img src="/mysql/uploads/<?php echo $imgrow['file']; ?>" class="img-responsive title-image center-block" /></a>
<div class="flat-price center-block"><?php echo $data['price']; ?><span class="glyphicon glyphicon-rub" aria-hidden="true"></span> </div>
<div class="flat-title"><?php echo $data['title'];?> <?php echo $data['id']; ?></div>
<div class="dimmed-text left"><?php echo $data['type']; ?> </div>
<div class="dimmed-text right"><div class="tooltip_my"><span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span><?php echo $data['area'];?> <span class="tooltiptext"><?php echo $data['address']; ?></span></div></div>
<br>
<div class="dimmed-text left"><div class="tooltip_my"> <span class="glyphicon glyphicon-phone" aria-hidden="true"></span><?php echo $data['phone'];?><span class="tooltiptext"><?php echo $data['name'];?></span></div></div>
<div class="dimmed-text right"> <?php echo $data['square'];?></div>
<table class="flats-table">
<tr>
<td colspan="3">
<button id="collapsar" class="btn btn-info pull-left" data-toggle="collapse" data-target="#hide-me<?php echo $tmpCount;?>"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> <span class="btntext">Показать миниатюры</span></button>
</span> <span class="btntext">Подробнее ...</span>
</td>
</tr>
<tr>
<td colspan="3">
<div id="hide-me<?php echo $tmpCount;?>" class="collapse">
<?
$imagestm2=$DB_con->prepare('SELECT file FROM flats_images WHERE post_id=:uid');
$imagestm2->bindParam(':uid',$number);
$imagestm2->execute();
for ($i = 1; $i <= $imagestm2->rowCount(); $i++) {
$imgrow2=$imagestm2->fetch(PDO::FETCH_ASSOC);
extract($imgrow2); ?>
<div class="col-xs-6 col-sm-6 col-md-4 col-lg-4">
<div class="thumbnail"> <img src="mysql\uploads\<?php echo $imgrow2['file']; ?>" data-toggle="modal" data-target="#myModal" class="img-responsive"/></div>
</div>
<?
} ?>
</div>
</td>
</tr>
</table>
</div><?
$tmpCount++;
}}
?>
</div>
Actual result i want: Actual result
I think you are simply trying to iterate over your results, generate 3 columns of output, and then also apply filters, maybe through Javascript, which hide some of your elements. The simplest way I know to do that is as follows:
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
<div class="col-xs-12 col-sm-6 col-md-6 col-lg-4"></div>
...
<!-- Bootstrap can manage with only 1 .row -->
</div>
I don't think it's possible to do, what i wanted to do, instead i found much easier way to display content: just unite 3 columns in one and put all data there. Ye, it has problems when you open the spoiler, but i think i'll deal with it. Thank you for all your answers.

Passing a PHP variable via ?page= within a single page web app

I have created a single page Webapp using Bootstrap 3. It looks and feels like the standard IOS contacts app. I am using it for personal data on contacts. The contacts are in a sql table which I am querying. I am trying to pass a the PHP mySql id result to an internal #details id element.
The link is:
name
The div is:
<article id="details">
<div class="container-fluid">
<?php
$therecord = $_GET['page'];
$result = $Con ->query("SELECT * FROM `myTable` WHERE `id` = '$therecord'");
$details_row = $result->fetch_array(MYSQLI_BOTH);
?>
<div class="row">
<a href="#on_duty"><div class="col-xs-1"><span class="glyphicon glyphicon-menu-left floatleft apple_back_chevron appleBlue" aria-hidden="true"></span></div>
<div class="col-xs-6"><p class="apple_back_text appleBlue">On Duty Coordinators</p></div></a>
<a href="#edit_coordinators?page=<?php echo $details_row['id'] ;?>"><div class="col-xs-4"><p class="apple_forward_text appleBlue floatright">Edit</p></div>
<div class="col-xs-1"><p class="glyphicon glyphicon-menu-right floatright apple_forward_chevron appleBlue" aria-hidden="true"></p></div></a>
</div>
</div>
<div class="container-fluid">
<div class="row">
<div class="col-xs-3">
<img class="img-circle" src="" alt="photo">
</div>
<div class="col-xs-9">
<h2><?php echo $details_row['name'];?></h2>
</div>
</div>
<div class="row">
<div class="apple_list appleGray">
<div class="col-xs-8">
<p class="appleBlue">station</p>
<p class="appleDGray"><?php echo $details_row['station']; ?></p>
</div>
<div class="col-xs-4">
</div>
</div>
</div>
<div class="row">
<div class="apple_list appleGray">
<div class="col-xs-8">
<p class="appleBlue">mobile</p>
<p class="appleDGray"><?php echo $details_row['mobile']; ?></p>
</div>
<div class="col-xs-2"> <img class="img-responsive apple_Glyph" src="_images/sms.png"></div>
<div class="col-xs-2"> <img class="img-responsive apple_Glyph" src="_images/phone.png"></div>
</div>
</div>
</div> <!-- /container -->
</article>
How do I pass a PHP "?page=$recordNo" internally? The page id is being passed, but not getting picked up by the next PHP
Try:
name
It is also very possible you could write it like so:
name
Basically, ? starts the query string and (if included) is always before the page anchor (fragment) # in a URI. See Uniform Resource Identifier for more info.
You can't do this:
name
Because there's an "hashtag", so it's interpreted by the browser as an in-page navigation, and the request isn't sent to the server, unless you catch it with javascript and then perform some ajax request...
This problem seems completely unrelated to PHP or MySQL.
Just look at the generated mark-up, which will look like this (re-indented to improve readability):
<a href="#edit_coordinators?page=333">
<div class="col-xs-4">
<p class="apple_forward_text appleBlue floatright">Edit</p>
</div>
<div class="col-xs-1">
<p class="glyphicon glyphicon-menu-right floatright apple_forward_chevron appleBlue" aria-hidden="true"></p>
</div>
</a>
There're two obvious issues:
The <a> tag is an inline element. It cannot have a whole bunch of block elements inside.
You inject the page GET parameter in the hash part of the URL, which is client-side only and is never sent to the server. It should be ?page=333#edit_coordinators
Said that, I admit I can't fully understand what you have in mind but it you really have a single-page application it has to be coded in JavaScript and possibly use AJAX. With just server-side technologies you can only
build a conventional web application.

how to make loop in php

i need to select data from table named booked from a given range of data and display data form a variable and then used it to select from another table and display the data that is selected but when the data that is selected from table booked is multiple only the first data is displayed in the variable here's my code:
$res1=mysqli_query($bd,"select * from booked where datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'");
$num1=mysqli_num_rows($res1);
if($num1>0)
{
for($y=0;$y<$row1=mysqli_fetch_assoc($res1);$y++)
{
$res=mysqli_query($bd,"select * from rooms where capacity>='$newcap' and room_number!='".$row1['roomnumber']."'");
while($row=mysqli_fetch_assoc($res))
{
echo'<div class="col-lg-4 col-md-4 col-sm-12">';
echo'<div class="newsBox">
<div class="thumbnail">
<figure><img src="reservation/img/rooms/'.$row['img'].'" width="230" height="150"></figure>
<div class="caption maxheight2">
<div class="box_inner">
<div class="box">
<a class="title"><strong>'.$row['name'].'</strong></p>
<b>'.$row['description'].'</b>
<p>'.$row['price'].'</p>
</div>
<a class="btn btn-default" href="info_pay.php?roomnumber='.$row['room_number'].'&roomtype='.$row['name'].'&from='.$_POST['from'].'&adult='.$_POST['adult'].'&child='.$_POST['child'].'&to='.$_POST['to'].'&roomprice='.$row['price'].'"><span class="glyphicon glyphicon-plus">Select this Room</span></a>
</div>
</div>
</div>
</div>';
echo'</div>';
}
}
}
You may want to to try a different approach using heredoc as it is less prone to quoting errors, here's a complete example to loop a mysqli query using heredoc.
<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="select * from booked where datefrom between '$from' and '$to' or dateto>='$from' and dateto='$to'";
if ($result=mysqli_query($con,$sql))
{
while ($row=mysqli_fetch_row($result))
{
echo <<< LOL
<div class="col-lg-4 col-md-4 col-sm-12">
<div class="newsBox">
<div class="thumbnail">
<figure><img src="reservation/img/rooms/{$row['img']}" width="230" height="150"></figure>
<div class="caption maxheight2">
<div class="box_inner">
<div class="box">
<a class="title"><strong>{$row['name']}</strong></p>
<b>{$row['description']}</b>
<p>{$row['price']}</p>
</div>
<a class="btn btn-default" href="info_pay.php?roomnumber={$row['room_number']}&roomtype={$row['name']}&from={$_POST['from']}&adult={$_POST['adult']}&child={$_POST['child']}&to={$_POST['to']}&roomprice={$row['price']}"><span class="glyphicon glyphicon-plus">Select this Room</span></a>
</div>
</div>
</div>
</div>
</div>
LOL;
// Free result set
mysqli_free_result($result);
}
}
//close mysqli connection
mysqli_close($con);
?>

Categories