This page is supposed to echo posts from the post table, and link them with images that have the same post_id in the images table. Each post can contain multiple images or no images at all. I want to be able to echo all the images that are linked to a specific post.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
include("db.php");
$select_post = "select * from post as p
union
select img from images as i
on i.post_id = p.post_id";
$run_post = mysqli_query($conn, $select_post);
while ($row_post=mysqli_fetch_array($run_post)){
$post_id = $row_post['post_id'];
$post_title = $row_post['post_title'];
$post_date = $row_post['post_date'];
$post_author = $row_post['post_author'];
$post_content = substr($row_post['post_content'],0,100);
$post_image = $row_post['img'];
$post_image .= '<img src="post_image/'.$post_image.'" width="60" height="60"/>';
?>
<tr align="center">
<td><?php echo $post_id; ?> </td>
<td><?php echo $post_date; ?></td>
<td><?php echo $post_author; ?></td>
<td><?php echo $post_title; ?></td>
<td><?php echo $post_image; ?></td>
<td><?php echo $post_content; ?></td>
<td><center><button>X</button></center></td>
<td><center><button>Edit</button></center></td>
</tr>
<?php
}
?>
First, your query should be something like:
$select_post = "SELECT p.*, i.img
FROM post p
LEFT JOIN (
SELECT
group_concat(img) as img,
post_id
FROM images
GROUP BY post_id
) i ON i.post_id = p.post_id";
// this query can be optimized a bit i think, but it should do the job.
Then, you need to $images = explode(',', $row_post['img']); inside your while to produce an array of images.
Loop over $images and process them as you need.
$post_image = '';
if(count($images) > 0){
foreach($images as $image){
$post_image .= '<img src="post_image/'.$image.'" width="60" height="60"/>';
}
}
Related
I've an csv file with plant code like X001, X005, X019...
In mysql table plantcode=username.
I want to fetch all the plantcodes from my DB table where username are not present in my CSV file and deleted =0.
mycode
<?php
$file = fopen("aa.csv","r");
echo "<pre>";
//print_r(fgetcsv($file));
?>
<tr>
<td width="10%"><label>Username</label></td>
<td width="10%"><label>service center id</label></td>
<td width="10%"><label>service center name</label></td>
</tr>
<?php
$arr= fgetcsv($file);
foreach ($arr as $k => $v) {
$q= "SELECT * FROM `service_center` WHERE `deleted` = 0 AND username NOT LIKE '$v'";
$r = mysql_query($q);
$nr = mysql_num_rows($r);
if ($nr > 0) {
WHILE ($rows = mysql_fetch_array($r)) {
?>
<td><?php echo $rows['username'] ?></td>
<td><?php echo $rows['service_center_id'] ?></td>
<td><?php echo $rows['service_center_name'] ?></td>
<?php
}
}
else{
?>
<tr>
<td><?php echo $k;?></td>
<td><?php echo $v; ?></td>
<td><?php echo "No service center found";}?></td>
</tr>
<?php
}
fclose($file);
?>
I get output for each query like this
If username=X008 then from CSV i dont get X008 for one loop and in the next loop I get X008 and ,dont get present value plant code, But I want something like this
select * from service_center WHERE username != "X001, X007.." AND deleted = 0;
use NOT IN($name_array)
Pass username array in NOT IN
select * from service_center WHERE username NOT IN ("X001", "X007") AND deleted = 0;
I did try some solutions from the same problem with mine but nothings working. I bet it's totally in my code. I'm trying to place them in every table that has been looped, It worked I was able to get a button on every table but I can't get them to work. Here's my code
<?php
$con = new PDO('mysql:host=localhost; dbname=library_member', $username = 'root', $password = '');
$q = "SELECT * FROM user_list";
$stmt = $con->prepare($q);
$stmt -> execute();
?>
<table border="1">
<td>ID</td>
<td>Firstname</td>
<td>Lastname</td>
<td>Address</td>
<td>Contact</td>
<td>Email</td>
<?php while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><?php echo $row['address']; ?></td>
<td><?php echo $row['contact']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><button><a href='library-delete.php?id=$id/'>Delete</a></button></td>
</tr>
<?php } ?>
</table>
And this is the code for where the deleting happens.
<?php
$con = new PDO('mysql:host=localhost; dbname=library_member', $username = 'root', $password = '');
$q = "DELETE FROM user_list WHERE id = :id";
$stmt = $con->prepare($q);
$stmt->bindParam( ':id' , $_GET['id'], PDO::PARAM_INT);
$stmt->execute();
echo "<script type='text/javascript'>
alert('Information have been deleted.');
window.location='library-showlist.php';
</script>";
?>
It would just directly send me on the prompt part. None of my input data is deleted.
Try to change your code like this:
library-delete.php?id=<?php echo $row['id']?>
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm recently started to learn php & mysql,
i wanna know what's wrong with this
function show(){
$query=mysql_query(" SELECT * FROM family ");
if (!$query) {
die("invalid query ".mysql_error());
}
while ($row=mysql_fetch_array($query)) {
echo "<tr>
line 24 ===><td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['lname']; ?></td>
</tr>";
}
}
as you can see i have a function for show my data fields,
and i have a table in another page that i want to by just including & calling the function,my variables appear in the table,but i keep getting this stupid error:
help plz
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in C:\xampp\htdocs\php\lib.php on line 24
for those who wanna suggest this below code:
echo "<tr>
<td>". $row['id'] ."</td>
<td>". $row['name'] ."</td>
<td>". $row['lname'] ."</td>
</tr>";
i did try this and it works but it wont show my date in the table rows
my purpose of this is that i want it to show my each data variables in a table row, any other good way you know?
Change <td><?php echo $row['id']; ?></td> to <td>".$row['id']."</td>
You're trying to bring php code inside a string, which won't work. Just concatinate the strings using the 'dot' operator. Take a look here for more information.
Edit as requested:
echo "<table>";
while ($row=mysql_fetch_array($query)) {
echo "<tr>
<td>".$row['id']."</td>
<td>".$row['name']."</td>
<td>".$row['lname']."</td>
</tr>";
}
echo "</table>";
Instead of echoing, you need to just close and reopen your PHP tags:
function show(){
$query=mysql_query(" SELECT * FROM family ");
if (!$query) {
die("invalid query ".mysql_error());
}
while ($row=mysql_fetch_array($query)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['lname']; ?></td>
</tr>
<?php
}
}
Alternately, without opening and closing:
function show(){
$query=mysql_query(" SELECT * FROM family ");
if (!$query) {
die("invalid query ".mysql_error());
}
while ($row=mysql_fetch_array($query)) {
echo "<tr>
<td>". $row['id'] ."</td>
<td>". $row['name'] ."</td>
<td>". $row['lname'] ."</td>
</tr>";
}
}
try this
function show()
{
$query = mysql_query("SELECT * FROM `family`");
//check if query failed
if (!$query) {
die("invalid query ".mysql_error());
}
//if there are more than 0 rows in table, we create the table
if (mysql_num_rows($query) > 0)
{
echo "<table>";
//loop thru array and create table rows
while ($row = mysql_fetch_array($query))
{
echo "<tr>
<td>". $row['id'] ."</td>
<td>". $row['name'] ."</td>
<td>". $row['lname'] ."</td>
</tr>";
}
echo "</table>";
}
else //no rows, we display an error
{
echo 'Table is empty';
}
}
also use mysqli_* because mysql_* is depreciated
Im trying to display a few rows of data for each <h3><?php echo $name; ?></h3> item that appears in the loop.
With the current code it only shows one row for each item <h3><?php echo $name; ?></h3>
I have this in my functions.php file
$newdb = new wpdb('login', 'pass', 'db', 'host');
<?php $result=$newdb->get_results('select tbl1.name, tbl2.col1, tbl2.col2, tbl2.col3 from tbl1, tbl2 where tbl1.name=tbl2.tbl1_name');
$names=array();
foreach($result as $row): ?>
<?php $names[$row->name][]=$row;
endforeach; ?>
<?php foreach($names as $name=>$info): ?>
<h3><?php echo $name; ?></h3>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
<?php foreach($info as $n):?>
<tr>
<td><?php echo $n->col1; ?></td>
<td><?php echo $n->col2; ?></td>
<td><?php echo $n->col3; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endforeach; ?>
So the loop displays the heading followed by a few rows of records, not just one.
<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
col1-value1 col2-value1 col3-value1
col1-value2 col1-value2 col1-value2
etc.
</table>
<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
col1-value1 col2-value1 col3-value1
col1-value2 col1-value2 col1-value2
etc.
</table>
<h2>Name</h2>
<table>
<tr><th>col1</th><th>col2</th><th>col3</th></tr>
col1-value1 col2-value1 col3-value1
col1-value2 col2-value2 col3-value2
etc.
</table>
.....`
It's possible using your idea but you can do it using one query like
global $wpdb;
$result=$wpdb->get_results('select a.col, b.col1, b.col2, b.col3 from a,b where a.col=b.col5');
Assume that you have tbl1 table and it has a name field and tbl2 table and it has a field for example tbl1_name which has the name value of tbl1 name field and other fields, (with no duplicates) so you can join both tables like
$result=$wpdb->get_results('select tbl1.name, tbl2.col1, tbl2.col2, tbl2.col3 from tbl1, tbl2 where tbl1.name=tbl2.tbl1_name');
$names=array();
foreach($result as $row): ?>
$names[$row->name][]=$row;
<?php endforeach; ?>
foreach($names as $name=>$info):
echo $name;
?><table><?php
foreach($info as $n):
?>
<tr>
<td><?php echo $n->col1; ?></td>
<td><?php echo $n->col2; ?></td>
<td><?php echo $n->col3; ?></td>
</tr>
<?php
<?php endforeach; ?>
?></table><?php
<?php endforeach; ?>
Also remember $wpdb is a global object and available through the template files so you dont need to use new to make an instanse, just use global keyword as I wrote in my answer.
Class Reference/wpdb and sql join and this one.
I have a script that displays information of these fields- batchname, class, batchinstructor from the table "batch". But I want to display dynamically generated serial number on the left side when I show the data. For example:
Serial Number BatchName Class Batch Instructor
1. Solar Class Five John
2. Lunar Class six Bon Jovi
I have tried a lot but its not working. Would you please kindly help me to solve this? Please note that these serial number are not from database.
Here's my Controller:
<?php
class Batchlist extends CI_Controller{
function index(){
$this->load->library('pagination');
$config['base_url'] = base_url().'batchlist/index';
$config['total_rows'] = $this->db->get('batch')->num_rows();
$config['per_page'] = 20;
$config['num_links'] = 20;
$config['full_tag_open'] = '<div class="pagination" align="center">';
$config['full_tag_close'] = '</div>';
$this->pagination->initialize($config);
$this->load->model('mod_batchlist');
$data['records']= $this->mod_batchlist->batch_list($config['per_page'],$this->uri->segment(3));
$data['main_content']='view_batchlist';
$this->load->view('includes/template',$data);
}
}
?>
Here's my Model:
function batch_list($perPage,$uri) {
$this->db->select('*');
$this->db->from('batch');
$this->db->join('teacher', 'batch.batchinstructor = teacher.teacherid');
$this->db->order_by('batchid','DESC');
$getData = $this->db->get('', $perPage, $uri);
if($getData->num_rows() > 0)
return $getData->result_array();
else
return null;
}
Here's my View
<h1>Batch List </h1>
<?php if(count($records) > 0) { ?>
<table id="table1" class="gtable sortable">
<thead>
<tr>
<th>Batch Name</th>
<th>Class</th>
<th>Batch Instructor</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php foreach ($records as $row){ ?>
<tr>
<td><?php echo $row['batchname'];?></td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?>support/images/icons/edit.png" alt="Edit" />
<img src="<?php echo base_url(); ?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
<div class="tablefooter clearfix">
<div class="pagination">
<?php echo $this->pagination->create_links(); ?>
</div>
</div>
Maybe I misunderstood, but...what about a simple counter?? (after you ordered your records in SQL).
You need to pass to the view the number of items per page (in this snippet: $per_page), then you retrieve the current page from the URI ($this->uri->segment(n)).
At page 1, counter starts from (20*0)+1, i.e. 1. At page 2, starts from (1*20)+1 ie 21, at page 3 from (2*20)+1 ie 41 and so on...
<?php
$cur_page = $this->uri->segment(n) ? intval($this->uri->segment(n)) : 1;
$i = (($cur_page-1) * $per_page) +1;
foreach ($records as $row) :
?>
<tr>
<td><?php echo $i;?>.</td>
<td><?php echo $row['batchname'];?></td>
<td><?php echo $row['class'];?></td>
<td><?php echo $row['teachername'];?></td>
<td> <img src="<?php echo base_url(); ?>support/images/icons/edit.png" alt="Edit" /><img src="<?php echo base_url();?>support/images/icons/cross.png" alt="Delete" />
</td>
</tr>
<?php
++$i;
endforeach;
?>
You can place the counter wherever you want, I added a column for simplicity but if you want it somewhere else just place the counter there.
You said the serial numbers are not from the database. Where are they from then? If you want the database to generate them you can create a column named id and set it as the primary key and have it auto increment.