Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to use PHP and MySQL together with a while and a foreach loop?
    primarykey
    data
    text
    <p>I'm trying to use PHP and MySQL to automatically update my <code>"Products"</code> page. I use MySQL to store my product's IDs, Product name, price, quantity etc. I need help getting PHP to take the MySQL data and put each ID into a <code>&lt;div&gt;</code> with a specific class.</p> <p>I figured I needed a <code>while()</code> loop to get the array of the data and a <code>foreach()</code> loop to get each instance of the ID, but I am not using the <code>foreach()</code> loop right.</p> <p>Please excuse my mess of coding. I'm trying to get all the concepts down and working before I look for more efficient ways of doing things. Note that the <code>sql2['id']</code> contains more than just one item.</p> <pre><code>&lt;?php mysql_connect('localhost', 'user', 'password'); mysql_select_db('store'); $sql1 = mysql_query(' SELECT c.id, c.name, c.description, c.price, c.quantity, c.itemid, c.imgname, c.position, (SELECT Count(t.id) FROM topics AS t WHERE t.parent = c.id AND t.id2 = 1) AS topics, (SELECT Count(t2.id) FROM topics AS t2 WHERE t2.parent = c.id AND t2.id2 != 1) AS replies FROM categories AS c GROUP BY c.id ORDER BY c.position ASC '); if($sql1 === false){ die(mysql_error()); } while($sql2 = mysql_fetch_array($sql1)){ foreach($sql2['id'] as $value){?&gt; &lt;div class="itemInset"&gt; &lt;p&gt;&lt;?php echo $sql2['name']; ?&gt;&lt;/p&gt; &lt;img src="admin/image/&lt;?php echo $sql2['imgName']; ?&gt;" alt="&lt;?php echo $sql2['imgName']; ?&gt;" /&gt; &lt;/div&gt;&lt;?php } } ?&gt; </code></pre> <p>The categories SQL:</p> <pre><code>CREATE TABLE IF NOT EXISTS `categories` ( `id` smallint(6) NOT NULL, `name` varchar(256) NOT NULL, `description` text NOT NULL, `price` text NOT NULL, `quantity` int(10) NOT NULL, `itemID` text NOT NULL, `cC` text NOT NULL, `imgName` text NOT NULL, `position` smallint(6) NOT NULL, PRIMARY KEY (`id`), KEY `name` (`name`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; </code></pre> <p>And a <code>var_dump()</code>:</p> <pre><code>array(20) { [0]=&gt; string(1) "1" ["id"]=&gt; string(1) "1" [1]=&gt; string(9) "PC" ["name"]=&gt; string(9) "PC" [2]=&gt; string(58) "computer " ["description"]=&gt; string(58) "computer " [3]=&gt; string(6) "150.00" ["price"]=&gt; string(6) "150.00" [4]=&gt; string(4) "1000" ["quantity"]=&gt; string(4) "1000" [5]=&gt; string(8) "PCR-1000" ["itemID"]=&gt; string(8) "PCR-1000" [6]=&gt; string(0) "" ["imgName"]=&gt; string(0) "" [7]=&gt; string(1) "1" ["position"]=&gt; string(1) "1" [8]=&gt; string(1) "0" ["topics"]=&gt; string(1) "0" [9]=&gt; string(1) "0" ["replies"]=&gt; string(1) "0" } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload