Convert code into foreach loop

Hello there this is the code which fetch images from database with reference of ID which fetch only a single images and i want to fetch images one after one which can be done with foreach loop so help to convert this code in foreach loop

<?php
	$con = mysql_connect('localhost','root',''); 
	if (!$con) { die('Could not connect: ' . mysql_error()); } 
	mysql_select_db("job", $con); 
	$result = mysql_query("SELECT file FROM describepost WHERE id='23'"); 
	echo "<table border='1' width='100%' >"; 
		while($row = mysql_fetch_array($result)) { 
				echo "<tr>";
				  echo "<td>" . $row['file'] . "</td>";
			echo "<td><img src='img/".$row['file']."'></td>";
			echo "</tr>"; 
		}
	echo "</table>";
	
	mysql_close($con);
	?>

so what did you try by yourself? i see no foreach in there.

even stranger that you have multiple images with the same ID… (or the query is wrong)

Do you need to select a specific range or group of image IDs? Or are you selecting all of the images in the table?

Let me exaplain i Am making a dashboard as i have specified earlier and i want to display images one after for every visitors and i think that it can be possible only with for each loop only so help me to convert this code into for each loop…

And i want to display all the images one after one after refreshing the page…

In this case you should remove the WHERE from the query, that will make one query select all the images, not just the one specified by the ID.
You may then iterate through the table rows using a while loop.

As you said i have removed the where and called it with while loop and not getting it so i request you to send me the full code with foreach loop as i am not getting it…

You do not make the query in the while loop. The while loop comes after the data has been pulled from the table, to iterate through the results.
This is very basic, standard use of sql in php, if you do not know how to do this, you really need to find a course or tutorial on the subject, preferably one using current methods rather than the obsolete mysql.
Though from what became apparent in your previous topic, perhaps you first need to learn html and get a firm basis in that before you even consider using php.

You seem to be mistaking the forums for a “free coding service”. This is a place where you will get help to learn how to code, not a place where people will do your work for you.

6 Likes

if you want images “one after another on each refresh” you have to store the ID of the last image anywhere. then easily query like

... where id > $lastid limit 1

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.