Hello.

There is a code:

<?php $res = mysql_query("SELECT * FROM `wp_organisations` WHERE `o_category`=121"); while($row = mysql_fetch_assoc($res)){ echo '<span>' .$row['o_name']. '</span> <span>' .$row['o_phones']. '</span> <span>' .$row['o_address']. '</span><br>'; } ?> 

it is used to list by category. However, there are many categories, and I don’t want to write a separate file for each list with the above contents. How to make so that in part "WHERE o_category = X" instead of X from the list of links to the categories would be substituted the value of the category to which the user moves? Would there be a single output template for list output? Thank.

    1 answer 1

    Make a page, and on it take the value of the current category through $ _GET, and accordingly substitute it in the query:

     <?php if(isset($_GET['id'])){ $res = mysql_query("SELECT * FROM `wp_organisations` WHERE `o_category`='".abs(intval($_GET['id']))."'"); if(mysql_num_rows($res)>0){ while($row = mysql_fetch_assoc($res)){ echo '<span>' .$row['o_name']. '</span> <span>' .$row['o_phones']. '</span> <span>' .$row['o_address']. '</span><br>'; } }else{ echo 'Error!'; } }else{ echo 'Error!'; } ?> 

    The category link will look like this: http://example.com/category.php?id=XX

    Where XX will be the category number.

    • Did I understand correctly? I edit my code as specified, and also make another page with a list of categories, where for each I place the link example.com/category.php?id=XX , where instead of XX I prescribe category numbers, right? - Aviko
    • @Aviko, yes - Elime
    • OK, thanks, I will try, I will unsubscribe. - Aviko
    • Thanks, it worked great! - Aviko