How can i create permanently accessible urls from database entries? - php

I am trying to make a mirror of the this site for my own country, but I am having a hard time comperehending how to create new urls from my database entries.
For example as on the site I linked, it has and id/name to identify that exacty site, i want to achieve the same to make seperate questions shareable.
My current code is only created to test with 1 question on localhost url.
Here's a little snippet of what I've currently got:
<?php
$id = 1;
$result = performQuery("SELECT * FROM dilemmas WHERE id = ".$id);
while($row = mysqli_fetch_array($result)){
$blue_question = utf8_encode($row['blue_question']);
$blue_votes = utf8_encode($row['blue_votes']);
$red_question = utf8_encode($row['red_question']);
$red_votes = utf8_encode($row['red_votes']);
}
?>
<div class="answer-peek">
<div class="peek-text">Vil du helst...</div>
<div class="peek-buttons">
<div class="peek-bluebutton"><?php echo $blue_question ?></div>
<div class="peek-redbutton"><?php echo $red_question ?></div>
</div>
</div>
<div class="row text-center" id="textrow">
<h3 style="color:white;">Vil du helst...</h3>
</div>
<div class="row" id="buttonrow">
<div class="col-md-1">
<div class="button-left"></div>
</div>
<div class="col-md-10"><
<div class="col-md-5 col-md-offset-1 button" id="bluebutton">
<div class="blueCheckDiv"></div>
<div id="text-container">
<table cellpadding="0">
<tbody>
<tr>
<td valign="middle">
<div class="result">
<div class="percentage">
<?php
$percent = $blue_votes/($blue_votes + $red_votes);
$percent_friendly = number_format( $percent * 100, 0 ) . '%';
echo $percent_friendly;
?>
</div>
<div class="total-votes">
<span class="count">
<?php echo $blue_votes; ?>
</span>
<span class="word">
enige
</span>
</div>
<div class="question-text">
<?php echo $blue_question ?>
</div>
</div>
<p class="question">
<?php echo $blue_question ?>
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="col-md-5 button" id="redbutton">
<div class="redCheckDiv"></div>
<div id="text-container">
<table cellpadding="0">
<tbody>
<tr>
<td valign="middle">
<div class="result">
<div class="percentage">
<?php
$percent = $blue_votes/($blue_votes + $red_votes);
$percent_friendly = number_format( $percent * 100, 2 ) . '%';
echo $percent_friendly;
?>
</div>
<div class="total-votes">
<span class="count">
<?php echo $red_votes ?>
</span>
<span class="word">
uenige
</span>
</div>
<div class="question-text">
<?php echo $red_question ?>
</div>
</div>
<p class="question">
<?php echo $red_question ?>
</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div class="col-md-1">
<div class="button-right"></div>
</div>
</div>
<div class="row" id="dividerrow">
<div class="col-md-8" id="divider-left"></div>
<div class="col-md-4" id="divider-right"></div>
</div>
<div class="row" id="socialrow">
<div class="col-md-8 socialcol" id="col-left"></div>
<div class="col-md-4 socialcol" id="col-right"></div>
</div>

I suggested you to use a framework in the comments to your question.
Anyway, to do what you are trying, you can try something like this:
<?php
$id = 1;
if (true === isset($_GET['id'])) {
$id = $_GET['id'];
}
$result = performQuery("SELECT * FROM dilemmas WHERE id = ".$id);
while($row = mysqli_fetch_array($result)){
$blue_question = utf8_encode($row['blue_question']);
$blue_votes = utf8_encode($row['blue_votes']);
$red_question = utf8_encode($row['red_question']);
$red_votes = utf8_encode($row['red_votes']);
}
And you can call this same page with something like example.com/page.php?id=1234.
So, you can build links for each question like this:
Share
NOTE: This is a really dirty code and it opens to a lot of security issues. I wrote it only to make you able to understand the dynamics, but, again, I suggest you to use a framework on the first start: make all in "plain" PHP will cause you to deal with a lot of complex problems like the security or the handling of routing or services, etc... Use a framework to find very good guidance and to learn faster and better the language of your choice (PHP in this case).

Related

Bootstrap Cards Filter PHP/AJAX Problems

I have a page of a website which has bootstrap cards on. The cards are generated dynamically, but I wanted to add a filter section. I am trying to use AJAX to acheive this, I have got to a point where when you select a checkbox it triggers the loader gif but then that loader gif doesn't disapear. Through my hours of debugging I can see that its the page that the AJAX call is calling that is the problem, but I just can't see what the issue is and I am not getting errors or anything in the console.
If anyone has any ideas I would really appreciate it, I have pasted my code below, also I am aware my sql queries are currently open to injection, just want to get it to work first.
<body>
<?php include("PHP/header.php"); ?>
<div class="container-fluid">
<div class="container" style="margin-top: 2%; text-align: center;">
<h1>Title goes here</h1>
Some tetx
<br/>
<br/>
<br/>
Filter Reviews:
<ul class="list-group">
<?php
$search = $conn->prepare("SELECT DISTINCT reviewcat FROM review_db ORDER BY reviewcat");
$search->execute();
while ($row = $search->fetch(PDO::FETCH_ASSOC)) {
?>
<li class="list-group-item">
<div class="form-check">
<label class="form-check-label">
<input type="checkbox" class="form-check-input product_check" value="<?=$row['reviewcat'];?>" id="reviewcat"> <?=$row['reviewcat']; ?>
</label>
</div>
</li>
<?php } ?>
</ul>
</div>
<br/><br/>
<div class="row-fluid ">
<h5 class="text-center" id="textChange"> All Products </h5>
<hr>
<div class="text-center">
<img src="Images/loader.gif" id="loader" width="100" style="display: none">
</div>
<div id="result" class="card-deck card_group_style pt-4" >
<?php
$res_data = $conn->prepare("SELECT * FROM review_db");
$res_data->execute();
?>
<?php while ($row = $res_data->fetch(PDO::FETCH_ASSOC)) {// Important line !!! Check summary get row on array .. ?>
<div class="col-sm-6 col-lg-3 py-2">
<div class="card mb-4">
<img class="card-img-top card-images " src="Images/Reviews/<?php echo $row['reviewimage1'];?>" alt="<?php echo $row['reviewtitle'];?>" >
<div class="card-body">
<h5 class="card-title"><?php echo $row['reviewtitle'];?></h5>
<p class="card-text"><?php echo $row['reviewsynop'];?></p>
<a href="Reviews/review-content.php?id=<?php echo $row['id'];?>&reviewtitle=<?php echo $row['reviewtitle'];?>" class="btn btn-primary my-4" >Read More</a>
<div class="card-footer" style="padding: 1%;">
<small class="text-muted">Submitted: <?php
$my_date = $row['reviewsub'];
$date = DATE("d/m/Y",strtotime($my_date));
echo $date;?></small>
</div>
</div>
</div>
</div>
<?php } ?>
</div>
</div>
</div>
<?php include("PHP/footer.php"); ?>
</div>
</body>
<?php include("PHP/js.php"); ?>
<script>
$(document).ready(function(){
$('#link-review,#link-footer-review').addClass('active');
});
</script>
<script type="text/javascript">
$(document).ready(function(){
function get_filter_text(text_id){
var filterData = [];
$('#'+text_id+':checked').each(function(){
filterData.push($(this).val());
});
return filterData;
}
$(".product_check").click(function(){
$("#loader").show();
var action = 'data';
var reviewcat = get_filter_text('reviewcat');
$.ajax({
method:'POST',
url:'reviewaction.php',
data:{action:action,reviewcat:reviewcat},
success:function(response){
$("#result").html(response);
$("#loader").hide();
$("#textChange").text("Filtered Reviews");
}
});
});
});
</script>
</html>
reviewaction.php
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
include_once $_SERVER['DOCUMENT_ROOT'] . '/Private/db.php';
// $stmt = $conn->prepare("SELECT * FROM blogdata ORDER BY blogsub DESC");
// $stmt->execute();
?>
<?php
if(isset($_POST['action'])){
$sql = "SELECT * FROM review_db WHERE reviewcat !=''";
$revsearch = $conn->prepare($sql);
$revsearch->execute();
$output='';
if($revsearch->num_rows>0){
while ($row = $revsearch->fetch(PDO::FETCH_ASSOC)) {
$output .= '
<div class="col-sm-6 col-lg-3 py-2">
<div class="card mb-4">
<img class="card-img-top card-images " src="Images/Reviews/'. echo $row['reviewimage1'];.'" alt="'. echo $row['reviewtitle'];.'" >
<div class="card-body">
<h5 class="card-title">'. echo $row['reviewtitle'];.'</h5>
<p class="card-text">'. echo $row['reviewsynop'];.'</p>
<a href="Reviews/review-content.php?id='. echo $row['id'];.'&reviewtitle='. echo $row['reviewtitle'];.'" class="btn btn-primary my-4" >Read More</a>
<div class="card-footer" style="padding: 1%;">
<small class="text-muted">Submitted: '.
$my_date = $row['reviewsub'];
$date = DATE("d/m/Y",strtotime($my_date));
echo $date;.'</small>
</div>
</div>
</div>
</div>
';
}
else{
$output = "<h3>No Reviews Found!</h3>";
}
echo $output;
}
?>
I have actually found something in the server error log, I was stupidly only use dev tools on firefox, but have found that in the reviewaction.php script I had left semicolons in and had also missed a closing bracket. There are still some other problems, which I can fix, just couldn't work out why it wasn't firing.

how to display 3 columns dynamic data from database using bootstrap 4

Following is my code I am displaying details as 3 columns per row using Bootstrap class row.
I tried like changing div and some condition
<div class="container">
<?php
if($lclResult->rowCount() > 0){
while($row = $lclResult->fetch(PDO::FETCH_ASSOC)) {
# code...
$lclUserName = $row['us_business_name'];
$lclImage1 = $row['us_image1'];
$lclCategory = $row['us_category'];
$lclArea = $row['us_area'];
?>
<div class="row">
<div class="col-sm-4">
<div class="card">
<img class="card-img-top " src="<?php echo $lclImage1 ?>" alt="Card image" style="width:100%; height: 158px;">
<div class="card-body">
<h4 class="card-title"><?php echo $lclUserName?></h4>
<p class="card-text" style="font-size: 25px;"><?php echo $lclCategory?></p>
<hr>
<i class="fa fa-map-marker" style="font-size: 23px;"><span> </span><?php echo $lclArea?></i>
<!-- See Profile -->
</div>
</div>
<?php
}
?>
</div>
<?php
} else {
?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1> NO RESULT FOUND...</h1>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
I want to display 3 columns per row to display data. If anyone knows Please guide me with the above code.
Change your code like this
<div class="container">
<div class="row">
<?php
if($lclResult->rowCount() > 0){
while($row = $lclResult->fetch(PDO::FETCH_ASSOC)) {
# code...
$lclUserName = $row['us_business_name'];
$lclImage1 = $row['us_image1'];
$lclCategory = $row['us_category'];
$lclArea = $row['us_area'];
?>
<div class="col-sm-4">
<div class="card">
<img class="card-img-top " src="<?php echo $lclImage1 ?>" alt="Card image" style="width:100%; height: 158px;">
<div class="card-body">
<h4 class="card-title">
<?php echo $lclUserName?>
</h4>
<p class="card-text" style="font-size: 25px;">
<?php echo $lclCategory?>
</p>
<hr>
<i class="fa fa-map-marker" style="font-size: 23px;">
<span>
</span>
<?php echo $lclArea?>
</i>
<!-- See Profile -->
</div>
</div>
</div>
<?php
}
?>
<?php
} else {
?>
<div class="container">
<div class="row">
<div class="col-sm-12">
<h1> NO RESULT FOUND...
</h1>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>

PHP SUM total in the database

Hi I am new in PHP but I want to Sum my amount and display it on the page currently I am doing like this but it is not showing.
<div class="col-md-4 col-lg-4">
<div class="panel panel-bordered panel-black">
<div class="panel-heading">
<h3 class="panel-title"><?php echo translate('Total Amount ');?></h3>
</div>
<div class="panel-body">
<div class="text-center">
<h1>
<?php
echo $this->db->select('SUM(amount) as Total');
$getData = $this->db->get_where('tablename',array('amount = '!=0))->first_row();
echo $getData->Total;
?>
</h1>
</div>
</div>
</div>
</div>
try this
<?php
$data = $this->db->get_where('tablename',array('amount = '=>1))->sum(amount)->result();
foreach($data as $row):
echo $row['amount']; //if array output
echo $row->amount; //object output
endforeach;
?>
<div class="col-md-4 col-lg-4">
<div class="panel panel-bordered panel-black">
<div class="panel-heading">
<h3 class="panel-title"><?php echo translate('Total Amount ');?></h3>
</div>
<div class="panel-body">
<div class="text-center">
<h1>
<?php
echo $this->db->select('SUM(amount) as Total');
$getData = $this->db->get_where('tablename',array('amount = '!=0))->first_row();
echo $getData->Total;
?>
</h1>
</div>
</div>
</div>
</div>

My App Says - Cannot use object of type stdClass as array, any ideas?

For some reason my application is giving me a PHP error saying
Cannot use object of type stdClass as array on line 45.
A little stumped on why this is happening. Any idea?
I have been dabbling with this for over an hour now. Searched around on here as well. I'm so stumped. I hope it's not something simple that I'm just missing. Any help?
<section class="inner-pageBanner" style="width:100%; height:300px;
background: url(/uploads/home.png) no-repeat center center; background-size:cover;">
</section>
<div class="banner-text">
<h2 style="margin-top:100px; font-size:80px; color:#333;">Search
Results</h2>
</div>
<section class="homeAnimation">
<div class="featured-products products-slider">
<div class="container">
<div class="col-md-12">
<?php
$seg2 = $this->uri->segment('2');
$search = (empty($seg2) ? $_POST['search'] : $seg2);
$itemsq = $this->db->query("SELECT * from `products` where `text` like '%" . htmlspecialchars(addslashes($search)) . "%'");
$items = $itemsq->result();
foreach($items as $item){
?>
<div class="col-md-6 featured-item">
<div class="col-md-12 featured-background">
<div class="col-md-4">
<img class="featured-img" src="<?php echo $item->image; ?>"/>
</div>
<div class="col-md-8">
<div class="row">
<div class="col-md-12">
<a class="featured-title"
href="/product/<?php echo urlencode($item->code) . '/' . slugify($item->text); ?>">
<h4><?php echo $item->text; ?></h4>
</a>
<p class="trunc-desc"><?php echo $item->text; ?></p>
</div>
</div>
<div class="row " style="margin-top:0px; margin-bottom:0px;">
<div class="col-md-12">
<h4 style="margin-top:0px; font-size:18px;">As Low As:
<span
style="display: inline; color:forestgreen">$<?php echo number_format($item['price'] + ($item['price'] * ($settings->markup / 100))); ?></span>
</h4>
<br/><a
href="/product/<?php echo urlencode($item->code) . '/' . slugify($item->text); ?>"
class="featured-buy-btn">Buy Now</a>
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
error in this line code
<?php echo number_format($item['price'] + ($item['price'] * ($settings->markup / 100))); ?>
....
$item['price']
change to
$item->price
alternatively you can convert your Mysql result to an associative array with this function db_result_to_array($îtems). Then you can access the values of the array with $item['key']. in the associative array the keys will be the column headers.

Split mysql data into 3 bootstrap md columns with php

im trying to divide some info retrieved from a mysql db to 3 columns ("col-md-4") without repeating the info. I have been have trouble with mysqli_fecth_assoc() beacuse it replicates the info in all columns. Any idea? Thanks in advance
<div class="container-fluid">
<div class="row">
<?php while($fila = mysqli_fetch_assoc($resultado)){ ?>
<div class="col-md-4">
<p><?php echo $fila["Nombre"];?></p>
<HR WIDTH="50%" SIZE="3">
</div>
<div class="col-md-4">
<p><?php echo $fila["Nombre"];?></p>
<HR WIDTH="50%" SIZE="3">
</div>
<div class="col-md-4">
<p><?php echo $fila["Nombre"];?></p>
<HR WIDTH="50%" SIZE="3">
</div>
<?php } ?>
</div>
the problem with thath code is that it repeats the same $fila["Nombre"] in all columns.
I think this code will help to get the expected result.
<?php
$tempFila = array();
while($fila = mysqli_fetch_assoc($resultado)){
$tempFila[] = $fila["Nombre"];
}
$count = count($tempFila);
?>
<div class="container-fluid">
<div class="row">
<?php for ($i = 0; $i < $count;) { ?>
<div class="col-md-4">
<p><?php echo (!empty($tempFila[$i])) ? $tempFila[$i] : ''; $i++; ?></p>
<HR WIDTH="50%" SIZE="3">
</div>
<div class="col-md-4">
<p><?php echo (!empty($tempFila[$i])) ? $tempFila[$i] : ''; $i++; ?></p>
<HR WIDTH="50%" SIZE="3">
</div>
<div class="col-md-4">
<p><?php echo (!empty($tempFila[$i])) ? $tempFila[$i] : ''; $i++; ?></p>
<HR WIDTH="50%" SIZE="3">
</div>
<?php } ?>
</div>
</div>

Categories