SQL NOT IN, != what to use? - mysql

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;

Related

Arrange data in tables?

I retrieve the data from the database and tried to implement them in the table, but now I don't know how to arrange them in the table. My view is:
<?php include('inc/header.php');?>
<div class="main">
<table>
<thead>
<tr>ID:</tr>
<th>Name:</th>
</thead>
<tbody>
<tr>
<?php foreach ($view as $row) :?>
<?php $i = 1;?>
<?php echo "<td>".$row->audio."</td>";?>
<?php echo $i++;?>
<?php endforeach;?>
</tr>
</tbody>
</table>
</div>
<?php include('inc/footer.php');?>
I just want to make the place id increase from one to the as many records and arrange them into one table.
Your foreach loop should be like this
<tbody>
<?php
$i = 1;
foreach ($view as $row)
{
echo "<tr>";
echo "<td>".$i."</td>";
echo "<td>".$row->audio."</td>";
$i++;
echo "</tr>";
}
?>
</tbody>
And <thead> should be
<thead>
<tr>
<th>ID:</th>
<th>Name:</th>
</tr>
</thead>
Try This Code
<?php
$i=1;
foreach ($view as $row)
{
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row->audio;?></td>
</tr>
<?php
$i++;
} ?>
You can try this
<?php $i = 1;?>
<?php foreach ($view as $row) :?>
<tr>
<?php echo "<td>".$i++."</td>";?>
<?php echo "<td>".$row->audio."</td>";?>
</tr>
<?php endforeach;?>
Try the following code:
<?php
$i=0;//place the initial value outside the loop
foreach ($view as $row)
{
$i++;//increment the value
?>
<tr>
<td><?php echo $i;//display the value ?></td>
<td><?php echo $row->audio;?></td>
</tr>
<?php
} ?>

echo multiple images from one array in PHP/HTML

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"/>';
}
}

Only single return value($data) will display codeigniter View page

I have a result.php page in view/result.php
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php foreach($result as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>
</body>
html table is currently working. It retrives data from my table testing and display it. The problem is echo part in below code, it doesn't work.
<?php echo 'Your new value is '.$name1;?>
<?php echo 'Your new value is '.$name2;?>
<?php echo 'Your new value is '.$name3;?>
From the same table testing. It shows an error in the error page it shows Undefined variable name1,name2,name3
controller/example.php
<?php
class Example extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->helper('url');
}
function index() {
$this->load->view('example_view');
$this->getvalue();
$this->edit_content();
}
function getvalue()
{
if ($this->input->post('submit')==true) {
$data1['name1']=$this->input->post('name1');
$data1['name2']=$this->input->post('name2');
$data1['name3']=$this->input->post('name3');
$this->load->view('result',$data1);
$this->load->model('Insert_model');
$this->Insert_model->uploaddata($data);
}
}
function edit_content()
{
$data = array();
$this->load->model('Selected_model');
$this->load->helper('url');
$data['result'] = $this->Selected_model->get_contents();
$this->load->view('result',$data);
}
}
?>
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php if(is_array($result)){ foreach($result as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; } ?>
</table>
<?php echo 'Your new value is '.$row->name1;?>
<?php echo 'Your new value is '.$row->name2;?>
<?php echo 'Your new value is '.$row->name3;?>
If your array is like in the following format.
$info = array
(
[0] => Array
(
[name1] => a
[name2] => b
[name3] => c
)
[1] => Array
(
[name1] => aa
[name2] => bb
[name3] => cc
)
[2] => Array
(
[name1] => aaa
[name2] => bbb
[name3] => ccc
)
[3] => Array
(
[name1] => aaaa
[name2] => bbbb
[name3] => cccc
)
);
Then
<body>
<table border="1">
<tr>
<th>name1</th>
<th>name2</th>
<th>name3</th>
</tr>
<?php foreach($info as $row): ?>
<tr>
<td><?php echo $row->name1; ?></td>
<td><?php echo $row->name2; ?></td>
<td><?php echo $row->name3; ?></td>
</tr>
<?php endforeach; ?>
</table>
The above loop will give you accurate result like.
name1 name2 name3
a b c
aa bb cc
aaa bbb ccc
aaaa bbbb cccc
Now the following code will generate an error because it does not know the following
$row->name1
$row->name1
$row->name1
The reason is that these are defined in the scope of foreach loop thats why its generate error.

php host status from php mysql database

Help me!!
Seem when i run the scripts, the output seems wrong :(
This is the output...
Here My Output
The 1st and 3rd entry is true, online
But the rest should be OFFLINE but the output shows all ONLINE
I think the problem is from foreach loop but I cannot figure out how to solve the problem
Please help me... :(
<table border="1">
<thead>
<tr>
<th>Entry</th>
<th>Host</th>
<th>Name</th>
<th>Location</th>
<th>Description</th>
<th>Availability</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM host_entry";
$no = 1;
//foreach($dbh->fetchAll(PDO::FETCH_ASSOC) as $row)
$dbhi = $dbh->query($sql);
foreach ($dbhi->fetchAll(PDO::FETCH_BOTH) as $data) {
?>
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $data['host_server']; ?></td>
<td><?php echo $data['host_name']; ?></td>
<td><?php echo $data['host_location']; ?></td>
<td><?php echo $data['host_desc']; ?></td>
<td>
<?php
$ip = $data["host_server"];
// Run the ping to the IP
exec ("ping -n 1 -w 1 $ip", $ping_output);
if(preg_match("/Reply/i", $ping_output[2])!==0) {
echo "<font color='green'><strong>Online!</strong></font>";
}
else if(preg_match("/Reply/i", $ping_output[2])===0){
echo "<font color='red'><strong>Offline!</strong></font>";
}
?>
</td>
</tr>
<?php
}
?>
</tbody>

Inserting delete button for each table not working

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']?>