I have a page listing articles from a database. When user clicks a tag label they are taken to a blogsbycategory.php page. This page gets the tag parameter from the url and returns all blogs that have this tag. On the right hand side of this page I have a list of tags. How do I change the GET so when a user click a tag in the list, the url parameter will update and they can view all blogs for that chosen tag.
http://example.com/articles/blogsbycategory.php?tag=People%20management
<?php
$tag = $_GET["tag"];
$sql = "select * from blogs where tag = '$tag' " ;
$rs = mysqli_query($connect, $sql);
//get row
$fetchRow = mysqli_fetch_assoc($rs);
?>
<h3>Tags</h3>
<?php
while($row = mysqli_fetch_array($result))
{
echo '<ul style="list-style: none;">';
echo '<li>' .$row["tagName"]. '</li><hr />';
echo '</ul>';
}
?>
Something like this:
<h3>Tags</h3>
<?php
while($row = mysqli_fetch_array($result))
{
echo '<ul style="list-style: none;">';
echo '<li>' . $row["tagName"] . '</li><hr />';
echo '</ul>';
}
?>
With jQuery something like this:
<h3>Tags</h3>
<?php
while($row = mysqli_fetch_array($result))
{
echo '<ul style="list-style: none;">';
echo '<li onclick="$('#articleContainer').load(' . $row["tagName"] . ')">' . $row["tagName"] . '</li><hr />';
echo '</ul>';
}
?>
<div id="art" style="width: 600px; height: 800px; border: 1px solid grey;"></div>
List them as links
<h3>Tags</h3>
<ul>
<?php while($row = mysqli_fetch_array($result)) { ?>
<li><?php echo htmlspecialchars($row["tagName"]); ?></li>
<?php } ?>
</ul>
And please quote the data before you use them in a SQL statement.
Related
i am making gallery in codeignitor. i have retrived list of image names from database on view page. now i want to get image on clicking name through ajax. i am not that much good with code. kindly help me with fetch gallery function. any help is much appreciated. thanks in advance.
this is my code for fetchng image names:
<?php
//$i=1;
foreach($data as $row)
{
?>
<img class="img-circle img-bordered-sm" src='<?php echo base_url();?>assets/uploads/<?php echo $row->imagefiles;?>' height='30px' width='30px'>
<span class="username">
<a id="view_gallery" class="view_gallery" href="javascript:void(0);" id="<?php echo $row->id;?>"><?php echo $row->name; ?></a>
</span>
<hr>
<?php }
?>
and this is my script, i need further help with code :
<script type="text/javascript">
$( ".view_gallery" ).click(function() {
id = $(this).attr('id');
/* alert(userid);*/
$.ajax({
url : '<?php echo base_url('fetch_gallery_data') ;?>',
method : 'post',
data : {id : id},
success : function(res){
$('.disp_gallery').html(res);
}
})
});
</script>
here is my fetch gallery function in controller ,
public function fetch_gallery_data()
{
$id = $this->input->post('id');
$where = $id;
$result = $this->App_model->gallery_model('gallery','id',$where);
$data = '';
if(count($result) > 0)
{
foreach ($result as $key => $value)
{
$files = json_decode($value->imagefile);
if($value->id > 0)
{
$data .= "<div class='col-md-3' style='border:1px solid gray; margin:20px; padding:20px; ' id='".$id."''>";
$data .= "<a >";
$data .= "<img src='".base_url()."assets\uploads/seo-business-quotes-6.jpg' height='150' width='150'><br>";
$data .= "</div>";
}
else
{
foreach ($files as $key => $value)
{
$data .= "<div class='col-md-3' style='border:1px solid gray; margin:20px; padding:20px; '>";
$data .= "<img src=".base_url()."assets\uploads/".$value." height='150' width='150'><br>";
$data .= "</div>";
}
}
}
}
else
{
$data .= 'No Records.';
}
echo $data;
}
Change the slashes you mentioned in the controller then try
$data .= "<img src='".base_url()."assets/uploads/seo-business-quotes-6.jpg' height='150' width='150'><br>";
Here also change slash and single quotes
$data .= "<img src='".base_url()."assets/uploads/".$value."' height='150' width='150'><br>";
I am trying to make a filter for a website for cars. I would like to be able to filter by exterior color. I use exterior to represent my color in the database. My UL puts all color from the database into the box and makes each one a induvial check box. Then I have my page that list of all cars through a SELECT * Statement and then I fetch my results through individual echo statements that are wrap and a div. I Do not want to display my exterior colors name next to each car on this page since I am designing a mobile version and trying to keep it clean. However I have tried to list them as induvial tags and I still get send to a blank page.
<ul class="list-group">
<?php
$sql = "SELECT DISTINCT exterior FROM newcars ORDER BY exterior";
$result = $conn->query($sql);
while($row=$result->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" name="" value="<?= $row['exterior']; ?>" id="exterior"><?= $row['exterior']; ?>
</label>
</div>
</li>
<?php
}
?>
</ul>
<div id="test">
<?php
$sql = "SELECT * FROM newcars";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
echo ("<a href='newcarindex.php?id={$row['id']}'><div class='car'>");
echo '
<tr>
<td>
<img src="data:image\jpeg;base64,'.base64_encode($row['photo']).'"/>
</td>
</tr>
';
echo "<h2 class=car-name>";
echo $row['name'];
echo "</h2>";
echo "<span class=stock>STOCK#";
echo $row['stock'];
echo "</span>";
echo "<h3 class=car-msrp>";
echo $row['msrp'];
echo "</h3>";
echo "</div></a>";
}
} else {
echo "There are no matching results!";
}
?>
</div>
this then leads into my ajax script
<script type="text/javascript">
$(document).ready(function(){
$(".product_check").click(function(){
$("#loader").show();
var action = 'data';
var class = get_filter_text('class');
var body = get_filter_text('body');
var exterior = get_filter_text('exterior');
$.ajax({
url:'action.php',
method:'POST',
data:{action:action,class:class,body:body,exterior:exterior},
success:function(response){
$("#test").html(response);
$("loader").hide();
}
});
});
function get_filter_text(text_id){
var filterData = [];
$('#'+text_id+':checked').each(function(){
filterData.push($(this).val());
});
return filterData;
}
});
</script>
and Finally use a action.php page to connect to my database and filter the results
include 'dbh.php';
if(isset($_POST['action'])) {
$sql = "SELECT * FROM newcars WHERE class !=''";
if(isset($_POST['exterior'])) {
$exterior = implode("','", $_POST['exterior']);
$sql .="AND exterior IN('".$exterior."')";
}
$result = $conn->query($sql);
$output='';
if($result->num_rows>0){
while($row=$result->fetch_assoc()){
$photo = base64_encode($row['photo']);
$output .= "<a href='newcarindex.php?id={$row['id']}'>
<div class='car'>
<tr>
<td>
<img src='data:image\jpeg;base64,{$photo}'/>
</td>
</tr>
<h2 class='car-name'>{$row['name']}</h2>
<span class='stock'>STOCK#{$row['stock']}</span>
<h3 class='car-msrp'>{$row['msrp']}</h3>
</div></a>";
}
echo $output;
}
} else {
echo "There are no comments!";
}
}
?>
The Problem I have is every time i click on a color to filter by my page comes up blank showing no results or errors. What am I doing wrong?
just looking at the action.php, upon a successful query, I don't see $output being echoed. Is this the complete code for the action.php?
include 'dbh.php';
if(isset($_POST['action'])) {
$sql = "SELECT * FROM newcars WHERE class !=''";
if(isset($_POST['exterior'])) {
$exterior = implode("','", $_POST['exterior']);
$sql .="AND exterior IN('".$exterior."')";
}
$result = $conn->query($sql);
$output='';
if($result->num_rows>0){
while($row=$result->fetch_assoc()) {
$photo = base64_encode($row['photo']);
$output .= "<a href='newcarindex.php?id={$row['id']}'>
<div class='car'>
<tr>
<td>
<img src='data:image\jpeg;base64,{$photo}'/>
</td>
</tr>
<h2 class='car-name'>{$row['name']}</h2>
<span class='stock'>STOCK#{$row['stock']}</span>
<h3 class='car-msrp'>{$row['msrp']}</h3>
</div></a>";
}
echo $output;
} else {
echo "There are no comments!";
}
}
?>
```
I'm having a hard time displaying the title and note on each row of my database. I want to display one row (with the title and note) after each
from a form in a page heading to the displaying of row datas page.
This is my code below:
//
Let's say that we have 4 rows of datas. In my code, I can only display the first row, because it keeps having the first row's data. This is because the form is in the first php file. Then after I submit the form, it's directed to this file, and it keeps getting the first row.
<?php $con=mysqli_connect("localhost","root","","task");?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<?php
$id=$row['id'];
echo ' ';
echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $row['title'] . '</button>';
?>
<?php
}?>
<?php $results = mysqli_query($con, "SELECT * FROM note"); ?>
<?php while ($row = mysqli_fetch_array($results)) { ?>
<div class="note" data-id="<?= $row['id'] ?>">
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<?php
echo '<br><br>';
echo '<div class="padding">'.$row['title'].'';
echo '<br><br><br><br>';
echo ''.$row['note'].'</div>';
?>
</div>
</div>
<?php
}?>
<?php
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
} ?>
The code below is not tested but it should be correct and give you a better idea of what you are doing right/wrong. I hope it helps..
<?php
$con=mysqli_connect("localhost","root","","task");
$results = mysqli_query($con, "SELECT * FROM note");
while ($row = mysqli_fetch_array($results)) { //starting your data row loop
$id=$row['id'];// you are creating a variable here but not using it two lines down.
echo ' ';
//YOUR OLD CODE echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $row['title'] . '</button>';
echo '<button class="call_modal" data-id="$id" style="cursor:pointer;">'. $id . '</button>';// use the variable "$id" that you created here
// You dont need the next two lines, you already did a query and have the data loaded in the "$results" array
/* $results = mysqli_query($con, "SELECT * FROM note");
while ($row = mysqli_fetch_array($results)) */
?> // close the php tag if you are going to switch to html instead of "echoing html"
/* OLD CODE <div class="note" data-id="<?= $row['id'] ?>"> you were missing the "php" in the php tags */
<div class="note" data-id="<?php echo $id; ?>"> // corrected code
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<?php //switching back to php so create your open tag again...
echo '<br><br>';
echo '<div class="padding">'.$row['title'].'';
echo '<br><br><br><br>';
echo ''.$row['note'].'</div>';// you dont NEED the '' before .$row.... unless you want it but its just essentially a blank string
?>
</div>
</div>
<?php
} // ending your "for each data row" here
?>
<?php
// PS you're not using this function anywhere unless its in ommited code?
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
} ?>
In your loop you're using mysqli_fetch_array, which returns an array with each element in that array containing the field value.
What you want is mysqli_fetch_assoc instead, this will return a hash which you can then use the way you're using it now.
Another thing is that you don't need to have 2 loops in there querying the database. And please indent your code, it makes it really hard for you and everyone else to read it.
Here is the updated/cleaned up version of your code. This has been tested, you can find a sample code with instructions to run on my Github here.
<?php
$con = mysqli_connect("localhost", "root", "", "task");
$results = mysqli_query($con, "SELECT * FROM note");
while ($row = mysqli_fetch_assoc($results)) {
$id = $row['id'];
echo ' ';
echo '<button class="call_modal" data-id="' . $id . '" style="cursor:pointer;">'. $row['title'] . '</button>';
?>
<div class="note" data-id="<?= $row['id'] ?>">
<div class="modal">
<div class="modal_close close"></div>
<div class="modal_main">
<?php
echo '<br /><br />';
echo '<div class="padding">' . $row['title'];
echo '<br /><br /><br /><br />';
echo $row['note'];
echo '</div>'
?>
</div>
</div>
</div>
</div>
<?php
}
?>
<?php
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
I have an SQL-database with many tables. Now I would like to create an input-form to be able to get data into the db without writing the entire sql-code every time. And this should work as follows:
All table names are listed in a drop-down menu. After having selected a table name, a new table with 4 columns is created automatically:
The first column of this table simply contains an increasing number.
The second column contains the field-names of the selected table.
In the third column there are empty input fields to enter the values for the database. Only in the third line (=product name) there is a drop-down menu with all product names from the main-table of the db.
The fourth column contains the data type (e.g. int or varchar)
All tables in the database have the same structure in the first 3 columns: the first column contains the table-id, the second column the foreign-key (=master_id) and the third column the product_name.
Up to this point, the script works well with the following 2 php-files (javasql.php and getuser.php):
javasql.php:
enter code here
<!DOCTYPE html>
<html>
<head>
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<select name="users" onchange="showUser(this.value)">
<option value="" class="optdrugs">please select</option>
<?php
include("files/zugriff.inc.php"); // database Access
$sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA = 'product'";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value="'. $row['TABLE_NAME'] . '">' .
$row['TABLE_NAME']. '</option>';
echo '<br>';
}
?>
</select>
</form>
<br>
<div id="txtHint"><b>Bitte Tabelle auswählen:</b>
<br>
<?php
if (isset($_POST["submit"])) {
$sent = $_POST['sent'];
$q = $_POST['tablename'];
$column_passed = unserialize($_POST['column']); // content of array
$column is passed from getuser.php
foreach ($_POST["insertvalue"] as $key => $value) {
echo $value . "<br>";
$werte[] = "'$value'";
}
$sql="INSERT INTO $q ($column_passed) VALUES (" .
implode(", ", $werte) . ")"; // data entry
mysqli_query($db, $sql);
if (mysqli_affected_rows($db) > 0) {
echo "<h3 style='color:blue'>successful</h3>";
} else {
echo "<h3 style='color:red'>not
successful</h3>";
}
}
?>
</div>
</body>
</html>
enter code here
getuser.php:
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 1px solid black;
padding: 5px;
}
th {text-align: left;}
</style>
</head>
<body>
<form id="formdatabase" name="formdatabase" action="javasql.php"
method="post">
<input type="hidden" name="sent" value="yes">
<?php
$q = strval($_GET['q']);
$con = mysqli_connect('localhost','root','','product');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM $q";
$result = mysqli_query($con,$sql);
$numcols = mysqli_num_fields($result); // gets number of columns in result table
$field = mysqli_fetch_fields($result); // gets the column names from the result table
$data_type_array = array(
1=>'tinyint',
2=>'smallint',
3=>'int',
4=>'float',
5=>'double',
7=>'timestamp',
8=>'bigint',
9=>'mediumint',
10=>'date',
11=>'time',
12=>'datetime',
13=>'year',
16=>'bit',
252=>'text',
253=>'varchar',
254=>'char',
246=>'decimal'
);
$data_type_array = array_flip($data_type_array);
echo "<table>";
echo "<tr>";
echo "<th>" . 'Nr' . "</th><th>" . 'Column names' . "</th>
<th>" . 'Values for db-entry' . "</th><th>" . 'Type' . "</th>";
echo "</tr>";
echo "<tr>";
$nr = 1;
for($x=0;$x<$numcols;$x++):?>
<td><?= $nr; ?></td>
<td><?= $field[$x]->name; ?></td>
<?= $column[] = $field[$x]->name; ?>
<td>
<?php
if ($field[$x]->name == 'Name') { // if-Beginn
?>
<select name="insertvalue[<?= $x; ?>]" id="insertvalue<?=
$x; ?>" size="1" onchange = "javascript:getSelectedRow()">
<?php
include("files/zugriff.inc.php");
$sql = "SELECT * FROM product_main ORDER BY Name";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
echo '<option class="optdrugs" value='. $row['Name'] . '>' .
$row['Name'] . '</option>';
echo '<br>';
}
?>
</select>
<?php
$name_option = "";
} else {
$name_option = "<input type='text' id='insertvalue" . $x . "'
name='insertvalue[" . $x . "]' size='50'>";
echo $name_option;
}
?>
</td>
<?php
$key = array_search($field[$x]->type, $data_type_array);
if($key !== false){
echo "<td>" . $key . "</td>";
}else{
echo "<td>" . $field[$x]->type . "</td>";
}
?>
<td><?= $field[$x]->type; ?></td>
<?= $nr = $nr + 1; ?>
</tr>
<?php endfor;
echo "</table>";
mysqli_close($con);
?>
<input type="hidden" name="tablename" value="<?= $q; ?>">
<input type="hidden" name="column" value="<?php echo htmlentities
(serialize($column)); ?>">
<input type="submit" value="Enter values" name="submit">
</form>
</body>
</html>
Since I need the master_id (= foreign key) in addition to the product-name for database entry, I would like to extend my script, so that the respective master_id is automatically sent to the input field in line 2, when a product-name is selected in line 3 ... without clicking a button. I tried to do this with javascript but it didn´t work. As far as I know, the solution would be to use AJAX but unfortunately, I am not very used to AJAX.
I would be more than happy, if someone could help me to solve this problem!
So I've made a database where people can upload pictures to. The pictures are linked to a place which is linked to a category.
I'm trying to create a gallery page that displays all the images, and then the categories available. When you click on a category it's meant to show all the pictures for that category.
I've got my categories in my database as well, which I'm using php to echo out:
<?php
include('includes/connectdb.php');
/* Selects id and name from the table 'category' */
$query = "SELECT id, name FROM category";
$result_category = mysqli_query($dbc,$query);
?>
<h1>Category</h1>
<!-- iterate through the WHILE LOOP -->
<?php while($row = mysqli_fetch_array($result_category)): ?>
<!-- Echo out values {id} and {name} -->
<button name="category[]" value=" <?php echo $row['id']; ?> "><?php echo $row['name'] . '<br />'; ?></button>
<?php endwhile; ?>
When a button is clicked I need it to run this php:
<?php
if(isset($_POST['category[]'])){
displayimage();
function displayimage()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("ssdb",$con);
$qry="SELECT pictures.name, pictures.image, pictures.place_id
FROM pictures
INNER JOIN sted
ON pictures.place_id = sted.id
INNER JOIN placecategory
ON sted.id = placecategory.place_id
INNER JOIN category
ON placecategory.category_id = category.id
WHERE placecategory.category_id = $row['id']";
$result=mysql_query($qry,$con);
while($row = mysql_fetch_array($result))
{
//var_dump($row);
echo '<img height="300" width="300" src="data:image;base64,'.$row["image"].' "> ';
echo '<p style="display:inline-block">'.$row["name"].' </p> ';
}
mysql_close($con);
}
}
?>
I found out that it should be possible with ajax, so I added the following to my ajax.php file:
<?php
if (isset($_POST['action'])) {
switch ($_POST['action']) {
case 'category':
category();
break;
}
}
function category() {
displayimage();
function displayimage()
{
$con=mysql_connect("localhost","root","");
mysql_select_db("ssdb",$con);
$qry="SELECT pictures.name, pictures.image, pictures.place_id
FROM pictures
INNER JOIN sted
ON pictures.place_id = sted.id
INNER JOIN placecategory
ON sted.id = placecategory.place_id
INNER JOIN category
ON placecategory.category_id = category.id
WHERE placecategory.category_id = $row['id']";
$result=mysql_query($qry,$con);
while($row = mysql_fetch_array($result))
{
//var_dump($row);
echo '<img height="300" width="300" src="data:image;base64,'.$row["image"].' "> ';
echo '<p style="display:inline-block">'.$row["name"].' </p> ';
}
mysql_close($con);
}
exit;
}
?>
And this to my gallery.php file where I'm displaying the pictures and categories.
<script>
$(document).ready(function(){
$('.button').click(function(){
var clickBtnValue = $(this).val();
var ajaxurl = 'ajax.php',
data = {'action': clickBtnValue};
$.post(ajaxurl, data, function (response) {
// Response div goes here.
alert("action performed successfully");
});
});
});
</script>
And lastly I changed my button to be
<?php while($row = mysqli_fetch_array($result_category)): ?>
<!-- Echo out values {id} and {name} -->
<input type="submit" class="button" name="category[]" value=" <?php echo $row['id']; ?> "><?php echo $row['name'] . '<br />'; ?>
<?php endwhile; ?>
I'm linking to the jquery library and I've tested the SQL statement that I need to run, and it works when I specify what the placecategory.category_id is.