473,769 Members | 4,052 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Categorised Links (mysql)

Hi there,

I've looked in many places for what i need, but i just
can't seem to find it. I keep ending up in ad-managing
systems, or link-trackers ...

I have 1 mySQL table called 'categories':
-> id (INT), category (VARCHAR)

And 1 called 'links':
-> id (INT), cat_id (INT), url (VARCHAR)

I think this part speaks for itself.
What i want is to order the categories alphabetically,
and have them 'populated'. (example below)

Categories without any links in them (yet) shouldn't show
up at all. I Just cannot figure out the query.

Any help out there?

Greetings Frizzle.
********* E X A M P L E **********

category: COOL SITES
- http://www.icecold.com
- http://www.freezing.com
- http://www.frost.com
- http://www.freezing.com

category: HOT SITES
- http://www.fire.com
- http://www.sunbathing.com

(hope that's clear)

Nov 8 '05 #1
4 1488
frizzle wrote:
Hi there,

I've looked in many places for what i need, but i just
can't seem to find it. I keep ending up in ad-managing
systems, or link-trackers ...

I have 1 mySQL table called 'categories':
-> id (INT), category (VARCHAR)

And 1 called 'links':
-> id (INT), cat_id (INT), url (VARCHAR)

I think this part speaks for itself.
What i want is to order the categories alphabetically,
and have them 'populated'. (example below)

Categories without any links in them (yet) shouldn't show
up at all. I Just cannot figure out the query.

Any help out there?

Greetings Frizzle.
********* E X A M P L E **********

category: COOL SITES
- http://www.icecold.com
- http://www.freezing.com
- http://www.frost.com
- http://www.freezing.com

category: HOT SITES
- http://www.fire.com
- http://www.sunbathing.com

(hope that's clear)

how about select c.category, l.url from categories c, links l where
l.cat_id = c.id order by category;

The result you then get is
cool sites http://www.icecold.com
cool sites http://www.freezing.com
osv etc mm

If you want the category name once and the belonging urls you have to
have a nested query (in the php-code)
You first select the categoryname, and then if the category has url's
you choose them as well.
I've done this with sections and subsections like this:

while($sectionr ow = mysql_fetch_arr ay($sections)){
print("<li>".$s ectionrow['name']); // this is your category
if($subsections = mysql_query("se lect sectionid, name from section
where parentID={$sect ionrow['sectionid']}")){
// this is your url, where parentID here equals your cat_id
while($subsecti on= mysql_fetch_arr ay($subsections )){
print("\n<ul>\n <li>".$subsecti on['name']."</li>\n</ul>\n</li>\n");
}
}
}print("</ul>");

Nov 8 '05 #2
Hmm thanks for the reply!
Your first example is what i have right now, and the result of the
second one
is what i want, only, if i get this right, this would mean i'll have to
run a
separate query for each category...
I'm kind of trying to avoid that ..
Is it possible?

Greetings Frizzle.

Nov 9 '05 #3
frizzle wrote:
Your first example is what i have right now, and the result of the
second one
is what i want, only, if i get this right, this would mean i'll have to
run a
separate query for each category...
I'm kind of trying to avoid that ..
Is it possible?


On Usenet, it's frowned upon to reply without quoting the previous
poster.

Running a new query for each category gets inefficient as soon as you
have more than a few categories. Why not just do this:

$result = mysql_query("SE LECT c.category, l.url " .
"FROM categories c, links l " .
"WHERE l.cat_id = c.id ORDER BY category;");
$previous_categ ory = null;

while ($row = mysql_fetch_arr ay($result)) {
if ($row['category'] != $previous_categ ory) {
echo "<h1>" . $row['category'] . "</h1>\n";
$previous_categ ory = $row['category'];
}

echo $row['url'] . "<br>";
}

This allows the database to retrieve the data all at once, without
reconnecting each time.

-- David

Nov 9 '05 #4
David Wahler wrote:
frizzle wrote:
Your first example is what i have right now, and the result of the
second one
is what i want, only, if i get this right, this would mean i'll have to
run a
separate query for each category...
I'm kind of trying to avoid that ..
Is it possible?


On Usenet, it's frowned upon to reply without quoting the previous
poster.

Running a new query for each category gets inefficient as soon as you
have more than a few categories. Why not just do this:

$result = mysql_query("SE LECT c.category, l.url " .
"FROM categories c, links l " .
"WHERE l.cat_id = c.id ORDER BY category;");
$previous_categ ory = null;

while ($row = mysql_fetch_arr ay($result)) {
if ($row['category'] != $previous_categ ory) {
echo "<h1>" . $row['category'] . "</h1>\n";
$previous_categ ory = $row['category'];
}

echo $row['url'] . "<br>";
}

This allows the database to retrieve the data all at once, without
reconnecting each time.

-- David

First sorry for not quoting. I have only recently discovered the
quoting part
in Google Groups.

Second, thank you so much for your code. This is just great! I would
have
never ever thought of this! Thanks a lot!

Greetings Frizzle.

Nov 9 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1426
by: Dave Moore | last post by:
Hi All, I hope you can give me a few pointers on this issue. I'd like visitors to my website to be able to enter text to be viewed on the site - a bit like a bulletin board or forum. I can do this for regular text easily by simply populating a 'TEXT' field within my MySQL database and grabbing it back when rendering the site using PHP. However, I'd also like visitors to be able to put in URL links or email items that can be clicked. How...
3
3792
by: ozarks | last post by:
I want to setup a database symbolic link within Windows XP to a network drive which has significanlty more space available and can't seem to get it working. Here is my setup.... - I am running MySQL 4.1.7 - I have tried this with both the 'mysqld-max' and 'mysqld-max-nt' servers - Per the MySQL manual (section 7.6.1.3) I setup a 'foo.sym' file in
4
1809
by: Spare Brain | last post by:
Hi, I recently installed SuSE 9.3 on x86, and it seems to contain scripts "apache2" and "mysql" in the /etc/init.d folder. However, these two (Apache or MySQL) are not running when I fresh reboot the machine. I am having to execute a "./apache2 start" and "./mysql start" every time I reboot. I am a newbie when it comes to unix admin, and I am trying to read up about the rc scripts and how they link into the scripts under the...
1
2414
by: chanshaw | last post by:
Alright so I got php running and installed i have mysql running and installed the thing im having a hard time with is having the php to call information from the mysql database. Im on Windows Vista Ultimate, I'm using iis7 here is the code of the php. <?php $Username = "Webuser"; $Password = "password"; $Database = "sample"; $Hostname = "localhost"; $MySQLConnection = mysql_connect($Hostname, $Username, $Password) ...
0
10215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3564
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.