473,395 Members | 1,905 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

creating a dynamic template

5
Forgive me if this is too long/too simple - I am new to this!

I have an htm page with a "post" form. This has 2 droplists, the variables of which are posted to a php page. This page queries a mySQL database and echoes in rows any matching records.

At present each result row lists the info from the database fields name, location and type. I had put an "a href=" around these results so if a row is clicked, it opened a seperate htm page with all the details of that particular file.

I have 1000 records on my database and currently have 1000 htm pages. This means I have to update1000 pages every time something changes.

What I would really like to do is have it so that when a row is clicked on, a php page is opened. this page is a template so that all the info that I need from the database is inserted into the page

By having a php template, I only need update 1 page as the info is different every time.

I enclose the code that I am using on the 2 php pages. Can anyone see why it isn't working? What do I need to do. I am right at the boundary of my understanding here!

List Page:

Expand|Select|Wrap|Line Numbers
  1. mysql_connect ($host, $user, $password)
  2. or die ('I cannot connect to the database
  3. because: ' . mysql_error());
  4.  
  5.  
  6. mysql_select_db ($db);
  7.  
  8.  
  9.  
  10. $town = $_POST["town"];
  11. $cuisine =$_POST["cuisine"];
  12.  
  13. if ($cuisine == "All Cuisines") {
  14.  
  15. $query = mysql_db_query($db, "SELECT *  FROM `trialrestaurants` 
  16. WHERE `posttown` LIKE CONVERT(_utf8 '$town' USING latin1) COLLATE latin1_german2_ci");
  17.  
  18. }
  19. else{
  20.  
  21. $query = mysql_db_query($db, "SELECT *  FROM `trialrestaurants` 
  22. WHERE `posttown` LIKE CONVERT(_utf8 '$town' USING latin1) COLLATE latin1_german2_ci 
  23. AND `cuisine` LIKE CONVERT(_utf8 '$cuisine' USING latin1) COLLATE latin1_german2_ci ");
  24.  
  25. }
  26.  
  27.  
  28. echo "<table width='100%' border='0'  bordercolor='#000000' cellpadding='4' cellspacing='0'>\n";
  29.  
  30. $id = $row["ID"]; 
  31. while ($row = mysql_fetch_array($query)) 
  32.  
  33.  
  34.  
  35.  
  36. echo " 
  37. <tr>\n<td align='left'><a href='details.php?id=$id'>".$row[name]."</td> 
  38. <td align='left'><a href='details.php?id=$id'>".$row[location]."</td> 
  39. <td align='left'><a href='details.php?id=$id'>".$row[cuisine]."</a></td> 
  40. \n</tr>\n"; 
  41.  
  42.  
  43. echo "</table>";
Template Page:
Expand|Select|Wrap|Line Numbers
  1. mysql_connect ($host, $user, $password)
  2. or die ('I cannot connect to the database
  3. because: ' . mysql_error());
  4.  
  5.  
  6. mysql_select_db ($db);
  7.  
  8. $id = $_GET["ID"]; $details_table = "trialrestaurants"; 
  9. $query = mysql_db_query($db, "SELECT * FROM 'trialrestaurants' WHERE 'ID' LIKE CONVERT(_utf8 '$id'USING latin1) COLLATE latin1_german2_ci ");
  10.  $query = mysql_query($query) or die(mysql_error()); 
  11. $numrows = mysql_num_rows($query); 
  12.  
  13. if ( $numrows > 0 ) { // if the id was found 
  14.      while ( $details = mysql_fetch_array($query) ) { 
  15.  
  16.           echo "Town: ". $details['town']; 
  17.           echo "Cuisine: ". $details['cuisine']; 
  18.      } 
  19. }else { // if it was not found 
  20.      echo "error: invalid id."; 
  21.  
Thanks in advance for any help or ideas.
Jun 30 '07 #1
7 1670
I have 1000 records on my database and currently have 1000 htm pages. This means I have to update1000 pages every time something changes.
Seriously? You have written 1000 htm pages? :O It's MUCH MUCH easier to use just one PHP page (like you're apparently trying now).

I enclose the code that I am using on the 2 php pages. Can anyone see why it isn't working? What do I need to do. I am right at the boundary of my understanding here!
Please tell us EXACTLY why it isn't working. Do you get any error messages? If so, what do they say? What makes you think it isn't working.

Although I'm not sure about that, you might get rid of the quotes when denoting a column or table name. So
SELECT * FROM `trialrestaurants`
WHERE `posttown` LIKE CONVERT(_utf8 '$town' USING latin1) COLLATE latin1_german2_ci
becomes

SELECT * FROM trialrestaurants
WHERE posttown LIKE CONVERT(_utf8 '$town' USING latin1) COLLATE latin1_german2_ci
Try if this makes any difference.

EDIT:
Also, in your template php:

[PHP]
$query = mysql_db_query($db, "SELECT * FROM 'trialrestaurants' WHERE 'ID' LIKE CONVERT(_utf8 '$id'USING latin1) COLLATE latin1_german2_ci ");
$query = mysql_query($query) or die(mysql_error());
[/PHP]

Leave out the second line. You are trying to do two queries and to make it even worse you are submitting the result of the first one as a query string to the second query. ;)

The PHP manual also states:
This function (mysql_db_query) is deprecated, do not use this function. Use mysql_select_db() and mysql_query() instead.
Jun 30 '07 #2
Roarke
5
thanks for your reply/question.

When I put my mouse over the link the following is what is displayed in the status bar:
www.website.com/details.php?id=

When I click the link all that shows on the page is:
query was empty

Thanks for helping
Jun 30 '07 #3
mwasif
802 Expert 512MB
You are using single quotes instead of backticks around table name and column. Use backticks or remove them altogether e.g.
[PHP]$query = mysql_db_query($db, "SELECT * FROM trialrestaurants WHERE ID LIKE CONVERT(_utf8 '$id'USING latin1) COLLATE latin1_german2_ci ");[/PHP]
Jun 30 '07 #4
Roarke
5
Thanks for these replies. But no change.

It seems to me that I am not creating the ID variable. The 1st page sends the query to the 2nd page which picks up the info and echos it fine.

What isn't happening is that the id capture so when I go to page 3 there is nothing being carried over. Am I making myself clear?

If I try this:
snippet:
Expand|Select|Wrap|Line Numbers
  1. echo "<table width='100%' border='0'  bordercolor='#000000' cellpadding='4' cellspacing='0'>\n";
  2. $id = $row["ID"]; 
  3.  
  4. while ($row = mysql_fetch_array($query)) 
I get query was empty,
but if I try this:
snippet:
Expand|Select|Wrap|Line Numbers
  1. echo "<table width='100%' border='0'  bordercolor='#000000' cellpadding='4' cellspacing='0'>\n";
  2.  
  3. while ($row = mysql_fetch_array($query)) 
  4.  
  5. $id = $row["ID"]; 
I get nothing at all.

It is this $id bit that is not working but I don't know why? or how to change it.

Ideas???
Jul 1 '07 #5
kovik
1,044 Expert 1GB
Sounds to me like you're not taking the suggestions correctly. That snippet you've given tells us nothing. What does your query look like now? Where is $query coming from?
Jul 1 '07 #6
Roarke
5
Volectricity,
I am sorry if it seems that I am not taking the advice correctly. I am trying to.
This is my first website, I am at the edge of my knowledge and if I seem slow or whatever, it is due to my lack of knowledge rather than anything else.
I only put snippets of the whole code I had previously posted as I didn't want to take too much space.

Here is the whole page I am having issues with.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $host = "host details";
  3. $user = "user details";
  4. $password = "password";
  5. $db = "database details";
  6.  
  7. mysql_connect ($host, $user, $password)
  8. or die ('I cannot connect to the database
  9. because: ' . mysql_error());
  10.  
  11. mysql_select_db ($db);
  12.  
  13. $town = $_POST["town"];
  14. $cuisine =$_POST["cuisine"];
  15.  
  16. if ($cuisine == "All Cuisines") {
  17.  
  18. $query = mysql_db_query($db, "SELECT *  FROM `trialrestaurants` 
  19. WHERE `posttown` LIKE CONVERT(_utf8 '$town' USING latin1) COLLATE latin1_german2_ci");
  20.  
  21. }
  22. else{
  23.  
  24. $query = mysql_db_query($db, "SELECT *  FROM `trialrestaurants` 
  25. WHERE `posttown` LIKE CONVERT(_utf8 '$town' USING latin1) COLLATE latin1_german2_ci 
  26. AND `cuisine` LIKE CONVERT(_utf8 '$cuisine' USING latin1) COLLATE latin1_german2_ci ");
  27.  
  28. }
  29.  
  30. echo "<table width='100%' border='0'  bordercolor='#000000' cellpadding='4' cellspacing='0'>\n";
  31.  
  32. $id =$_row["name"];
  33.  
  34. while ($row = mysql_fetch_array($query)) 
  35.  
  36. echo " 
  37. <tr>\n
  38. <td align='left'><a href='details.php?name=$id'>".$row[name]."</td> 
  39. <td align='left'><a href='details.php?name=$id'>".$row[location]."</td> 
  40. <td align='left'><a href='details.php?name=$id'>".$row[cuisine]."</a></td> 
  41. \n</tr>\n"; 
  42.  
  43. echo "</table>";
  44. ?>
  45.  
What I know does work is the town and cuisine searches. I know this because if I use the form that posts to this page I get rows of information echoed out as it should.

What isn't happening is the $id capture for the next page.

Before, when I had an "a href" straight to an htm page the links worked. Now when I click the link it does not seem to find $id variable.

I notice that the status bar is incomplete when I mouse over the link. I get "...details.php?name= " Surely there should be something after that equals.

Is line 32, $id=$_row["name"]; in the wrong place. Is the line wrong or do I need more.

Thanks if you can help
Jul 2 '07 #7
kovik
1,044 Expert 1GB
Is line 32, $id=$_row["name"]; in the wrong place. Is the line wrong or do I need more.
Yes, it is. You have tried to define $id as "$row['name']", but $row doesn't even exist yet. In fact, not once does your code check any variable for existence before attempting to use it.

Either put "$id=$_row["name"];" INSIDE of your loop, or instead of using "$id," just use "$row['name']."
Jul 2 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Venkatesh | last post by:
Hi Everybody, This is the first timw I am entering into this Group. I am developing a VB Project with an MDI form. I want to display IE Favorites into my application. For this I need to...
0
by: Ex-Em-El | last post by:
I have a problem in creating a dynamic table in the same xml : 1.xml: <?xml version="1.0" standalone="yes"?> <?xml-stylesheet type="text/xsl" href="2.xsl"?> <aaa...
1
by: Manu | last post by:
Hello, i ve again a little problem with my dynamic web site... i need to insert a dynamic link (css) into my body ! Why ? because i've a html template and i insert into the body a dynamic...
0
by: Reed | last post by:
Can someone stear me in the right direction to convert a binary tree from a Linked List to a Dynamic Array... Dynamic arrays aren't something im strong with. //********bintree.h******** #ifndef...
1
by: Hasani \(remove nospam\) | last post by:
The way the system works is, you create a user control (ascx) that will be a template and must implement the interface IPageTemplate. You then create one or more user controls (ascx) that implement...
1
by: David C | last post by:
I am trying to dynamically create a datagrid with code that looks exactly like what the asp tags show below. It has only one column and that column is a template column with a link button control....
2
by: Patrick | last post by:
I want to define a set of web-form templates in XML and render the equivalent web-form with ASP.NET, then process any input server controls on the form. Reading the XML file from Page_load is...
5
by: Daniel Frey | last post by:
Hello I'd like to match a dynamic node, given as a parameter to the stylesheet. Something like: <xsl:stylesheet ...> <xsl:param name="tomatch"/> <xsl:template match="{$tomatch}"> Hallo...
1
by: lxyone | last post by:
Using a flat file containing table names, fields, values whats the best way of creating forms / html pages? I want control over the html pages ie 1. layout 2. what data to show 3. what...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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,...

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.