Jump to content

Displaying certain variables


kemper

Recommended Posts

I want to be able to display results of a certain variable.  My variable would be a field containing a certain result.  Since two of my fields contain preset results from a dropdown menu, the choices are simple.

If my table consists of:
[table border="0" cellpadding="0" style="border-collapse: collapse" width="100%" id="table1"]
  [table][tr]
    [td][b]age[/b][/td]
    [td][b]division[/b][/td]
    [td][b]team[/b][/td]
  [/tr]
  [tr]
    [td]7[/td]
    [td]A[/td]
    [td]Team 1[/td]
  [/tr]
  [tr]
    [td]8[/td]
    [td]A[/td]
    [td]Team 2[/td]
  [/tr]
  [tr]
    [td]8[/td]
    [td]B[/td]
    [td]Team 3[/td]
  [/tr]
  [tr]
    [td]7[/td]
    [td]A[/td]
    [td]Team 4[/td]
  [/tr]
  [tr]
    [td]9[/td]
    [td]A[/td]
    [td]Team 5[/td]
  [/tr]
  [tr]
    [td]8[/td]
    [td]A[/td]
    [td]Team6[/td]
  [/tr]
[/table]

How would I be able to allow my visitors to select to see all content of rows with 7 in "age" field of table?

My overall intentions are to have an option to have visitor select which "age" and show content of the selected "age" via selection from dropdown menu or something to that effect.

Assistance is greatly appreciated!
Link to comment
Share on other sites

Ok, you'll have to base your query off of the user input.

Your first query to show all the results in your table show's all ages. Add another query to create your drop-down menu such as:

$q = mysql_query("select distinct age from table_name");

that'll give you 7,8, and 9 only once.

then, when a user submits their age selection, base your new query from that lika so:

[code]
if (isset($_GET['age'])) {
  $age = (int)$_GET['age'];
  $query = msyql_query("select * from table_name where age='$age'");
}[/code]

Then you can loop through your results as normal
Link to comment
Share on other sites

I was told that I could code something like this:

[code]<?php

  include("config.inc.php");

  if ($db == 1) {
    echo "<center><font face='Arial' size='4' color='#000000'><b>Results:</b></font></center><br><br>\n\n";
  }
 
  if($db==1) {
    // mySQL Table
    $db_con = mysql_connect(MyHost, MyUser, MyPassword) or die("Connetion to database failed!");
    mysql_select_db(MyDatabase);
$age = $_GET['age'];
$sql = "SELECT age, division, team FROM MyTable";
$result = mysql_query($query) or die(mysql_error());
$i = 0;

echo "<table width='500' border='0'><tr>
        <th>Age></th><th>Division</th><th>Team</th></tr>";

while ($row = mysql_fetch_array($sql) {
echo "<tr><td>" . $row['age'] . "</td><td>" . $row['division'] . "</td><td>" . $row['team'] . "</td></tr>";

}
echo "</table>\n";
  }

  // mySQL ends

?>[/code]

This got me error:
[code]Parse error: syntax error, unexpected '{' in /home/kemper/public_html/MySite/view.php on line 21[/code]

Any suggestions?
Link to comment
Share on other sites

For starters, there is no WHERE clause in your query so it doesn't matter what you access the page with, it will allways pull all records.

Seconds, "Query was empty" does not appear anywhere in your code.

Please post the relevent code.
Link to comment
Share on other sites

[code]<?php

  include("config.inc.php");

  if ($db == 1) {
    echo "<center><font face='Arial' size='4' color='#000000'><b>Results:</b></font></center><br><br>\n\n";
  }
 
  if($db==1) {
    // mySQL Table
    $db_con = mysql_connect(MyHost, MyUser, MyPassword) or die("Connetion to database failed!");
    mysql_select_db(MyDatabase);
$age = $_GET['age'];
$sql = "SELECT age, division, team FROM MyTable";
$result = mysql_query($query) or die(mysql_error());
$i = 0;

echo "<table width='500' border='0'><tr>
        <th>Age></th><th>Division</th><th>Team</th></tr>";

while ($row = mysql_fetch_array($sql)) {
echo "<tr><td>" . $row['age'] . "</td><td>" . $row['division'] . "</td><td>" . $row['team'] . "</td></tr>";

}
echo "</table>\n";
  }

  // mySQL ends

?>[/code]

This code gave me that message.
Link to comment
Share on other sites

I am new to this as you can tell.

I am modifying scripts that I found elsewhere.  This all started with a PHPForm.  Results of the form drop into MySQL and a data.dat.  I was assuming that $1 = 0 was from it and don't know enought about it to remove.  No one else told me to remove it or tell what it is.

Im afraid so, about the error message.  I just checked it again.  Copy and pasted from this posting, updating my MySQL info and received that message agin.
Link to comment
Share on other sites

That code can not generate that error. It is that simple.

If you want to access the pages using something like the address you showed, try this...

[code]
<?php
  include("config.inc.php");
  if ($db) {
    echo "<center><font face='Arial' size='4' color='#000000'><b>Results:</b></font></center><br><br>\n\n";
    mysql_connect(MyHost, MyUser, MyPassword) or die("Connetion to database failed!");
    mysql_select_db(MyDatabase);
    $age = $_GET['age'];
    $sql = "SELECT age, division, team FROM MyTable WHERE age = '$age'";
    if ($result = mysql_query($query)) {
      if (mysql_num_rows($result) > 0) {
        $row = mysql_fetch_array($sql);
echo "<table width='500' border='0'><tr><th>Age></th><th>Division</th><th>Team</th></tr>";
        echo "<tr><td>" . $row['age'] . "</td><td>" . $row['division'] . "</td><td>" . $row['team'] . "</td></tr>";
echo "</table>\n";
      } else {
        echo "No results match $age";
      }
    } else {
      echo "Query failed: ". mysql_error() .": $sql";
    }
  }
?>
[/code]
Link to comment
Share on other sites

[quote]Dammit!  I need to take a class in this stuff.[/quote]

No such things I should imagine. Everyone with anything to do with php is self taught. It wouldn't hurt to read [url=http://hudzilla.org/phpwiki/index.php?title=Main_Page]this[/url] though.
Link to comment
Share on other sites

I must be mistyping something in my Database configurations, because I am still not getting results, but I am not getting errors either.  That is a good thing.

I am getting:
"No results match 7"

When my table is populated and I keep looking at it to make sure my field names are spelled right and not capitalized along with MyHost, MyUser, MyPassword, MyDatabase and MyTable.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.