hello,

I'm kind of new to the PHP-scene and I'm having difficulties with my first php-site

the situation:
I'm making a website were people view newsitems,
these newsitems are stored in a database ("postgresql")
every newsitem is stored in a category.

i would like to create a list of every category item in my database, so far my code looks like this:

<?php
include "connectie_db.php";
function showcategorie(){
	include "connectie_db.php";
	
	echo '<aside id="sidebar1">';
	echo "<h3>Categorie</h3>";
	echo "<ul>";
	for ( $counter = 1; $counter <= 10; $counter += 1) {
	$ZoekNaam = "Select cat_name from tovanu.categories where cat_id = '$counter'";
	$res = $db->query($ZoekNaam);
		
        if(MDB2::isError($res)){
					echo "code: ".$res->getUserInfo();
					exit();
        }

	
	$row = $res->fetchRow();
	echo "<li><a href='#'>$row[0]</a></li>";
	}
	
	echo '</ul>';
	echo '</aside>';
	
}
?>

problem number 1:

for ( $counter = 1; $counter <= 10; $counter += 1)

how can i make a for or a while loop so i can show al my categorie items?

problem number 2:

"<li><a href='#'>$row[0]</a></li>"

how can i make a the link, so i can view the newsitems within that category?
( something within the link, ? or something like that)
please explain your thoughts

any help would be appreciated!

You are currently executing 10 queries (one in every iteration). Instead, do NOT use a for construct and instead execute ONE query. IF you want ALL the records, do NOT use a WHERE clause - simply execute: Select cat_id, cat_name from tovanu.categories If you want only the records with categories 1 through 10 then use: Select cat_id, cat_name from tovanu.categories WHERE (cat_id>=1 AND cat_id<=10) then to process the results:

while($row = $db->fetchRow(MDB2_FETCHMODE_ASSOC)){
  echo '<li><a href="viewer.php?id=' . $row['cat_id'] . '">'.$row['cat_name'].'</a></li>';
}

These might help you as well:
http://www.installationwiki.org/MDB2#A_Quick_Example
http://w3story.tistory.com/entry/How-to-use-PHP-and-PEAR-MDB2-Tutorial

thx! great help

Glad to help.

Regards,
Hielo

PS: Don't forget to mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.