How to write query for same table same index group - php

combine one SQL queries from same table same index in PHP MySQL but i just try index name news image select table than than fetch this output view.
this output view image get to databese both are image but two two grouping view
how can its sole query throw
[first image get to databese][1] [third image get to databese][3] [five image get to databese][5] five 7 img ...............
[second image get to databese][2] [fourth image get to databese][4] [six image get to databese][6] six 8 img...............
but i try this query but no work
$numrow =0;
$vali=0;
$sql_q = mysqli_query($conn,"SELECT * FROM tbl_news where cat_id='$cat_id' and cat_id !=19 order by nid DESC");
$ny = mysqli_fetch_array($sql_q);
if mysqli_num_rows($sql_q) > 0) {
while($newsRowi = mysqli_fetch_array($sql_q)){
$row[]=$newsRowi;
}
$n=0;$val=0;
foreach($row as $newsRow ){
$news_img = $newsRow['news_image'];
$f_id = $newsRow['nid'];
$news_heading = substr($newsRow['news_heading'],0,50);
$sq = mysqli_query($conn,"SELECT * FROM tbl_news where nid='ny[0]'");
$ya=mysqli_fetch_array($sq);
$val = $ya['news_image'];
$n++;
$n++;
}
?>
please help me

Related

Select data from the database before or after a defined value

I am having problems achieving the query to select data from a table in the db after a defined value has been met.
My code to do this is:
$fi = 'first_segment'
$im = popo.jpg
$sqls = "SELECT * FROM $fi,news_pictures
WHERE $fi.pi_id = news_pictures.pi_id
AND news_pictures.i_name = '$im'
GROUP BY news_pictures.id DESC";
I wasn't able to achieve the result with that query.
Basically, I want the query to confirm if news_pictures.i_name = '$im' and if true, return starts from the value of $im followed by other data in the table depending on news_pictures.id DESC.
The sample data and output:
Table news_pictures:
id i_name
-- ------
1 coco.jpg
2 lolo.jpg
3 popo.jpg
4 dodo.jpg
Since $im = popo.jpg, I want my query to display all values starting from popo.jpg with id DESC, i.e. popo.jpg, lolo.jpg, coco.jpg.
I got to solve the question with the help of a friend.
$fsqls = "SELECT * FROM $fi,news_pictures WHERE $fi.pi_id = news_pictures.pi_id AND news_pictures.i_name = '$im' GROUP BY news_pictures.id";
$rres = mysqli_query($connection, $fsqls) or print(mysqli_error($connection));
while($row = mysqli_fetch_assoc($rres))
{
$rnm = $row["id"];
}
$sqls = "SELECT * FROM news_pictures WHERE news_pictures.id <= $rnm ORDER BY news_pictures.id DESC";

SELECT duplicate columns in MYSQL database PHP

I have a database table titled products.
I have 2 columns in the table titled name and image
I have 4 entries in my database
item 1 | image1.jpg
item 2 | image1.jpg
item 3 | image1.jpg
item 4 | image2.jpg
I need to display the results on a page but I only need to display image.1jpg once, my while() loop is obviously displaying all 4 images.
I've been at this for hours, please help!
My code:
$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' ORDER BY id ASC");
while($fetch = mysql_fetch_assoc($query)){
$image = $fetch['image'];
$name = $fetch['name'];
echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';
}
What you need is using GROUP BY in your query. So change the query as
SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC
Try the code below;
$query = mysql_query("SELECT * FROM products WHERE page='Birthdays' GROUP BY image ORDER BY id ASC");
while($fetch = mysql_fetch_assoc($query)){
$image = $fetch['image'];
$name = $fetch['name'];
echo ''.$name.'';
echo '<img src="'.$thumbnail_image.'"/>';
}
You can also use a trick like not using while statement at all. If you just write the
$fetch = mysql_fetch_assoc($query) without while you would just get the first result in the query. I hope this helps.

PHP Get next result from MySQL

I have a database that stores image urls andother data, I want a gallery to display them but because the site is public I may want to delete some images. When a user uploads an image source and title, it is inserted into the database with an auto increment unique ID. The current script only adds or subtracts 1 from the ID to display the next or previous image. Is there any better way of doing this?
My current script:
function image_control($id){
$next=$id+'1';
$prev=$id-'1';
$query = "SELECT MAX(id), MIN(id) FROM mp_images";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$max = $row['MAX(id)']; $min = $row['MIN(id)'];
}
if($next==$max+"1"){$next=$min;} if($prev==$min-"1"){$prev=$max;}
echo "<a href='".$next."'>Next Image</a> <a href='".$prev."'>Previous Image</a>";
}
An example of this failing would be images where the ID's are 3,4,5,7,8.
some pseudo code for getting the next id:
$nextId = SELECT id FROM mp_images WHERE id > $currentId ORDER BY id ASC LIMIT 1
if ( no results )
// no next image
and for the previous:
$prevId = SELECT id FROM mp_images WHERE id < $currentId ORDER BY id DESC LIMIT 1
if ( no results )
// no previous image
It's up to you on what to do if there is no next image (you could for instance fetch the first image) or no previous image (you are on the first image).

slow results using while loop with mysql_fetch_array

I'm getting terribly slow results from the following code. I've been trying to troubleshoot it for hours but with no results. I've included only the relevant code.
Edit:
I am trying to select an id from a database and then use that id to get all the images associated with it. I am then narrowing that group of images down to one. Once I have that image I am resizing it through an external file.
I've tried removing various parts of the code to identify the problem and it seems as if the slow down is being caused by the second query but I am not sure why. Thanks for your help.
$getworks = mysql_query ("SELECT a_id from artists where display_works = '1' and active = '1' order by project_year desc, fullname desc");
while ($getworksrow = mysql_fetch_assoc($getworks)){
$totalimages=1;
$addstyle = "";
$id = $getworksrow["a_id"];
$getimages = mysql_query ("SELECT a_id, image_id from images where a_id = '". $id ."' order by position asc LIMIT 1");
$getimagesrow = mysql_fetch_assoc($getimages);
foreach ($getimagesrow as $getimagesrows){
extract($getimagesrow);
if($totalimages > 1){ $addstyle = 'style="display:none;"'; }
else {
$myimagename = "http://artist.com/$a_id/images/$image_id" . "_large.jpg";
list($width, $height, $type, $attr) = getimagesize("$myimagename");
$myimagename = "http://artist.com/artists/resize.php/$a_id/images/$image_id" . "_large.jpg?resize(157x2000)";
if($getworksrows["layout"] == "vert"){$pl = "_vertical";}else if($getworksrows["layout"] == "website"){$pl = "-s";}else if($getworksrows["layout"] == "video"){$pl = "_video";}else{$pl = "_horizontal";}
echo "<li class='thumbnail_container' $addstyle> <a class='thumbnail' href=\"../works$pl.php?a_id=" . $getworksrows["a_id"] . "\"><span><img src=\"$myimagename\" /></span>\n</a></li>\n";
}
$totalimages++;
}
}
It's a a big performance overhead to execute queries like this specially when parent query have large no. of records.
You should use join artists table with images table and get all data by single query.
Later on make 2D array of per artists and images. and loop according to 2D array to display data
Below is join query you should use:
SELECT * from artists as art
left join images as img on art.a_id=img.a_id
where display_works = '1' and active = '1'
order by project_year desc, fullname desc
In While make data array:
while ($getworksrow = mysql_fetch_object($getworks)){
$data['a_id']['img_id']=$getworksrow->image; //Make 2D array
........
........
}
looping and display data :
foreach($data as $id=>$images)
{
foreach($images as $val){
// Do your stuff for displaying data
}
}
So please do required changes.

how to check current position in mysql

how can i check current number in mysql where....
my query is
$aid = 16;
$get_prev_photo = mysql_query("SELECT * FROM photos WHERE album_id='$aid' AND pic_id<'$picid' ORDER BY pic_id LIMIT 1");
$get_next_photo = mysql_query("SELECT * FROM photos WHERE album_id='$aid' AND pic_id>'$picid' ORDER BY pic_id LIMIT 1");
while i am getting current photo with following query
$photo = mysql_query("SELECT * FROM photos WHERE pic_id='$picid' LIMIT 1");
and getting total photos in album with following query
$photos = mysql_query("SELECT * FROM photos WHERE album_id='$aid'");
$total_photos = mysql_num_rows($photos);
now i want to check where i am and show it as Showing 1 of 20, showing 6 of 20 and so on...
now i want to check where i am actually...
i think you are referring to pagination, which can be achieved using LIMIT and OFFSET sql
decide the number of results you want per page, then select that many
create links like:
View the next 10
and dynamically change those every time
queries look ~like~
$offset=$_GET['view'];
SELECT * FROM table WHERE `condition`=true LIMIT 5 OFFSET $offset
this translates roughly as
select 5 from the table, starting at the 10th record
This is bad:
$photos = mysql_query("SELECT * FROM photos WHERE album_id='$aid'");
Because it grabs all the fields for the entire album of photos when all you really want is the count. Instead, get the total number of photos in the album like this:
$result = mysql_query("SELECT count(1) as total_photos FROM photos
WHERE album_id='$aid'");
if ($result === false) {
print mysql_error();
exit(1);
}
$row = mysql_fetch_object($result);
$total_photos = $row->total_photos;
mysql_free_result($result);
Now you have the count of the total number of photos in the album so that you can set up paging. Let's say as an example that the limit is set to 20 photos per page. So that means that you can list photos 1 - 20, 21 - 40, etc. Create a $page variable (from user input, default 1) that represents the page number you are on and $limit and $offset variables to plug into your query.
$limit = 20;
$page = $_POST['page'] || 1; // <-- or however you get user input
$offset = $limit * ($page - 1);
I'll leave the part where you code the list of pages up to you. Next query for the photos based on the variables you created.
$result = mysql_query("SELECT * FROM photos WHERE album_id='$aid'
ORDER BY pic_id LIMIT $limit OFFSET $offset");
if ($result === false) {
print mysql_error();
exit(1);
}
$photo_num = $offset;
while ($row = mysql_fetch_object($result)) {
$photo_num++;
$pic_id = $row->pic_id;
// get the rest of the variables and do stuff here
// like print the photo number for example
print "Showing photo $photo_num of $total_photos\n";
}
mysql_free_result($result);
I'll leave better error handing, doing something with the data, and the rest of the details up to you. But that is the basics. Also I did not check my code for errors so there might be some syntax problems above. To make a single photo per page just make $limit = 1.

Categories