/    Sign up×
Community /Pin to ProfileBookmark

php/mysql how-to question…

hey everyone…

I have a database. It may contain several hundred individual items, in one or more subclass:

art-print-type/artist/item

so, for example, art-print-type=bird / artist = joeschmoe

What I want to do is use PHP to build the links.

On the main menu, the user selects bird prints. The PHP then:
opens the database, grabs the bird table, finds each unique subclass (artist), and builds a link – the link launches another query using the subclass (artist) as a WHERE clause.

If I pound my head on the table for an hour, I could probably come up with somthing exceptionally convoluted that might or might not work…

Help please?

John

to post a comment
PHP

9 Comments(s)

Copy linkTweet thisAlerts:
@Daot_LagorilleFeb 19.2004 — Hmmm... your question is a bit vague, but I think I get the gist of whast you are trying to do.

Essential it sounds like you want to create dynamic page content driven by choices the user makes.

So, the link "bird" could call the next page, with a url value of "class=bird" or something. That page has a query on the database as you mentioned, with the "where class='$varClass"".

You can then display all the artists from that query with a select dinstinctrow in the select part of the querey, and have each of those links go to a new page, passing the artist name as a new url variable "artist=$row['artist']", as well as the existing "class" variable.

That new page will take the two url varibales, and slap them into the query, and booyah, there is your content.

Is this what you mean?
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 19.2004 — [i]Originally posted by Daot Lagorille [/i]

[B]Hmmm... your question is a bit vague, but I think I get the gist of whast you are trying to do.



Essential it sounds like you want to create dynamic page content driven by choices the user makes.



So, the link "bird" could call the next page, with a url value of "class=bird" or something. That page has a query on the database as you mentioned, with the "where class='$varClass"".



You can then display all the artists from that query with a select dinstinctrow in the select part of the querey, and have each of those links go to a new page, passing the artist name as a new url variable "artist=$row['artist']", as well as the existing "class" variable.



That new page will take the two url varibales, and slap them into the query, and booyah, there is your content.



Is this what you mean? [/B]
[/QUOTE]


YES.

Click main menu 'birds', page generated has a link for each artist (subclass) in the 'birds' table. User selects artist (joe blow) and page is generated a query listing items FROM birds WHERE subclass=joeblow.

is that clearer?
Copy linkTweet thisAlerts:
@Daot_LagorilleFeb 19.2004 — Kind of - I am thinking now that you may have a database design issue.

This totally depends on the structure of your info, but why not have all records in one talbe, with an additional field of "class".

So instead of:

Table name birds with fields recordID, artist, and other_field

Table name other_class with same fields above

Have one table called "stuff" with these fields:

recordID, class, artist, other_field

the class feild will then contain an identifier analagous to the old class table names.

Now, you could create a simple search function on one page where the user fills out a form with select menus for "class" and "artist", and clicks submit.

That sends the info to a new page with a query that select from the one new table where class = user_input_class and artist = user_input_artist.
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 19.2004 — [i]Originally posted by Daot Lagorille [/i]

[B]Kind of - I am thinking now that you may have a database design issue.



This totally depends on the structure of your info, but why not have all records in one talbe, with an additional field of "class".



So instead of:



Table name birds with fields recordID, artist, and other_field

Table name other_class with same fields above



Have one table called "stuff" with these fields:



recordID, class, artist, other_field



the class feild will then contain an identifier analagous to the old class table names.



Now, you could create a simple search function on one page where the user fills out a form with select menus for "class" and "artist", and clicks submit.



That sends the info to a new page with a query that select from the one new table where class = user_input_class and artist = user_input_artist. [/B]
[/QUOTE]


whoa. I'm really like 2 weeks into php/mysql... so relating databases together is not a concept that I fully grasp.

Here is my DB structure:
[CODE]
Database
-----table birds
field item#, field artist, field description, (etc)

-----table portrait
field item#, field artist, field description, (etc)
[/CODE]


so, the main menu button selects the TABLE.

The PHP on the next page then uses that TABLE, and browses through it to find each unique artist. HTML links are created, one for each unique artist. (thats the part I'm needing help with...) Those links launch the query:

[CODE]
function make_fill_table($table_name) {
$sql = $sql = 'SELECT * '
. ' FROM '. $table_name
. ' WHERE subclass = "audubon"';

$result = mysql_query($sql);
if (!$result) {
print mysql_error() . "ERROR - query failed";
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
/* and etc... */
[/CODE]


I already have the query written to write the table of items out the way I want it. See [URL=http://jtest.stegenga.net/querytest2.php]here.[/URL]


is that clearer?
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 20.2004 — what is the proper way to format a distinctrow query?
Copy linkTweet thisAlerts:
@Daot_LagorilleFeb 20.2004 — That would be:

[code=php]SELECT DISTINCTROW row_name FROM table_name WHERE blah blah blah...[/code]

Further to your other post, it is not so much relating two databases together, insofar as it would benefit you, I think, to redesign your database.

Because your two tables are identical except for containing differently classified information, it will become a lot easier to use if you put everything in one table that is exactly like the others, but contains one extra field itentifying the classification - i.e. the value of the field is either "portrait" or "birds".

That way, the link for birds on page 1 could be:
[code=php]href="page02.php?class=birds"[/code]
Page two has this variable:
[code=php]
$varClass = "unset";
if (isset($HTTP_GET_VARS['class'])) {
$varClass = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['class'] : addslashes($HTTP_GET_VARS['class']);
}[/code]

And a query like this:
[code=php]sprintf("SELECT * FROM table_name WHERE class = '%s'"), $varClass;[/code]

You then display these records with a do/while loop. Each record will have a link to page 3 that looks like this:
[code=php]href="page03.php?class=birds&artist=<?php echo $row_ThingQuery['artist']; ?>"[/code]

Then page three does the same as page two, but with the added artist variable...
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 20.2004 — [i]Originally posted by Daot Lagorille [/i]

[B]That would be:



[code=php]SELECT DISTINCTROW row_name FROM table_name WHERE blah blah blah...[/code]

Further to your other post, it is not so much relating two databases together, insofar as it would benefit you, I think, to redesign your database.

Because your two tables are identical except for containing differently classified information, it will become a lot easier to use if you put everything in one table that is exactly like the others, but contains one extra field itentifying the classification - i.e. the value of the field is either "portrait" or "birds".

That way, the link for birds on page 1 could be:
[code=php]href="page02.php?class=birds"[/code]
Page two has this variable:
[code=php]
$varClass = "unset";
if (isset($HTTP_GET_VARS['class'])) {
$varClass = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['class'] : addslashes($HTTP_GET_VARS['class']);
}[/code]

And a query like this:
[code=php]sprintf("SELECT * FROM table_name WHERE class = '%s'"), $varClass;[/code]

You then display these records with a do/while loop. Each record will have a link to page 3 that looks like this:
[code=php]href="page03.php?class=birds&artist=<?php echo $row_ThingQuery['artist']; ?>"[/code]

Then page three does the same as page two, but with the added artist variable... [/B][/QUOTE]


So, you're saying to collapse all my tables:

[CODE]
DATABASE
------- item# / Picture Type / Artist / Item Description / (etc)
[/CODE]


but what does this line do:

[code=php]
$varClass = "unset";
if (isset($HTTP_GET_VARS['class'])) {
$varClass = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['class'] : addslashes($HTTP_GET_VARS['class']);
}[/code]

[/QUOTE]


and this one?

[code=php]sprintf("SELECT * FROM table_name WHERE class = '%s'"), $varClass;[/code]
[/QUOTE]


and how do I output those hrefs then?

John
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 20.2004 — Ok. This query runs against my database and returns one instance of each unique artist:

[code=php]
SELECT DISTINCT subclass FROM ornith [/code]


Now I have to figure out how to push that into a URL...

I'm getting there!

I figure a WHILE loop... but how to 'test' the while condition?

John
Copy linkTweet thisAlerts:
@BigjohnauthorFeb 20.2004 — Getting so close... I think...

In mysqladmin this query works:
[CODE]
SELECT DISTINCT subclass '
. ' FROM ' . $table_name
[/CODE]


and it returns the (currently) 2 unique subclasses...

But my PHP test query does not work as I think it should...
[code=php]
$table_name='ornith';
$dbh=mysql_connect ("localhost", "xxxxx_joetest", "xxxxx") or die ('Cannot connect to Database - error: ' . mysql_error () );
mysql_select_db("xxxxx_joetest1",$dbh);
$sql = 'SELECT DISTINCT subclass '
. ' FROM ' . $table_name ;
$result= mysql_query($sql);
echo "Query String= $result";
while($row=mysql_fetch_array($result)){
$content .=<<<END
<a href="http://jtest.stegenga.net/?table={$table_name}&subclass={$row}"></a>
END;
return $content;
echo "Content= $content";
}
[/code]


It does not fail, it just does not echo anything for the content variable....

HELP?

Thanks in advance

John
×

Success!

Help @Bigjohn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...