Jump to content

Archived

This topic is now archived and is closed to further replies.

vbracknell

Please help a newbie!

Recommended Posts

I'm trying to get a list to return information when it's clicked on. Index.php returns the list of all newspapers. That part works fine.

I need to set something up so that when the user clicks on the name of a newspaper that does not have a website, it takes them to another "mini-page." 

Can anyone point me in the right direction? I've just bought two new php/mysql books, so I'll be deep in that, but if anyone can point me in the right direction I'd really appreciate it!

Victoria

I was thinking I would have to change the field newspapername to something like:
a href="http://www.mspress.org/newspapers/item.php?id=1">newspaper</a
but I am not positive I think thinking along the right lines here.

index.php:
[code]<?php
$db = mysql_connect("localhost", "database", "password" );
mysql_select_db("tablename",$db);
$result = mysql_query("SELECT * FROM newspapers WHERE 1 ORDER BY City ASC",$db);
echo "<table border=0 cellspacing=5 cellpadding=0><font size='2'>\n";
echo "<tr><td>Type</td><td>Newspaper</td><td>Circ.</td><td>City</td><td>Phone</td><td>Fax</td><td>E-Mail</td><td>County</td><td>Membership</td><td>MCAN</td></tr></tr>\n";
while ($myrow = mysql_fetch_row($result)) {
      printf("<tr><td nowrap>%s</td><td nowrap>%s</td><td nowrap>%s</td><td nowrap>%s</td><td nowrap>%s</td><td nowrap>%s</td><td nowrap>%s</td><td nowrap>%s</td><td nowrap>%s</td><td nowrap>%s</td>\n",
      $myrow[1], $myrow[2], $myrow[3], $myrow[5], $myrow[8], $myrow[9], $myrow[11], $myrow[12], $myrow[13], $myrow[14]);
}
echo "</font></table>\n";
?>[/code]

item.php:
[code]<?php

$db = mysql_connect("localhost", "mydatabase", "mypassword" );
mysql_select_db("tablename",$db);
**/I know this isn't right
$result = mysql_query("SELECT *  FROM `newspapers` WHERE `Newspaper` LIKE 'Amory Advertiser'",$db);

echo "<table border=0 cellspacing=5 cellpadding=0>\n";
while ($myrow = mysql_fetch_row($result)) {

      printf("<tr>
                  <td>Type:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>Newspaper:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>Circulation:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>Address:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>City:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>State:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>Zip:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>Phone:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>Fax:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>Website:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>E-mail:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>County:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>Membership:</td>
                    <td>%s</td>
                  </tr>
                  <tr>
                    <td>MCAN/MDAN:</td>
                    <td>%s</td>
                  </tr>\n",
$myrow[1], $myrow[2], $myrow[3], $myrow[4],$myrow[5], $myrow[6],$myrow[7],$myrow[8], $myrow[9], $myrow[10],$myrow[11], $myrow[12], $myrow[13], $myrow[14]);
}
echo "</font></table>\n";
?>[/code]

MOD edit: code tags added

Share this post


Link to post
Share on other sites
Does your "newspapers" table have an "id" column?

Also, does it have a "url" column, with entries for those papers with a site?

Share this post


Link to post
Share on other sites
yes to both questions

The first field is the id - key field, set to autoincrement.

Yes, I do have a URL field, I just didn't display it on the index page.

Share this post


Link to post
Share on other sites
[code]I need to set something up so that when the user clicks on the name of a newspaper that does not have a website, it takes them to another "mini-page."[/code]

If not the url column, which you say you aren't using, how do know a paper doesn't have a site?

Share this post


Link to post
Share on other sites
I'm sorry, I wasn't very clear, was I?

The table has the following structure. The 'newspaper' field is clickable - has the name of the newspaper name and link in it if they have a website, otherwise it's just the name.

id  tinyint(4)       
NType  varchar(6)  
Newspaper  varchar(125)      
Circ  bigint(11) UNSIGNED      
Address  varchar(20)      
City  varchar(20)      
State  char(2)      
Zip  varchar(10)      
Phone  varchar(20)      
Fax  varchar(20)      
Website  varchar(100) <-- sort of redundant  
Email  varchar(100)    
County  varchar(15)  
Member  varchar(20)    
MCAN  char(2)    
MDAN  char(2)  

Share this post


Link to post
Share on other sites
Store the newspaper name in newspaper and the site address in website (eg www.newyorktimes.com)

Then

[code]<?php
$sql = "SELECT id, Newspaper, Website, City FROM newspapers ORDER BY City";
$res = mysql_query($sql) or die(mysql_error());

echo '<table>';
while (list ($id, $newspaper, $website, $city) = mysql_fetch_row($res))  {
// if has website, use it, else use item.php
$url = $website ? 'http://'.$website : "item.php?id=$id";
echo "<tr>
              <td><a href='$url'>$newspaper</a></td>
              <td>$city</td>
  </tr>";
}
echo '</table>';
?>[/code]

EDIT mysql_fetch_array() changed to mysql_fetch_row()

Share this post


Link to post
Share on other sites
Oops. Change my

mysql_fetch_array($res))
to
mysql_fetch_row($res))

if you are using my list() mthod.

Share this post


Link to post
Share on other sites
Well, I really thought I could figure the rest out, but I was wrong. With your help, I made the changes to the fields, and here's the new index.php file.

In advance, thank you for your time! I've been going through books all night!

Victoria

<?php
include("header.php");
include("links.php");

$db = mysql_connect("localhost", "user", "pass" );
mysql_select_db("database",$db);

$sql = "SELECT id, ntype, newspaper, circ, city, web, email, phone, fax, county, member, mcan FROM newspapers ORDER BY city";
$res = mysql_query($sql) or die(mysql_error());

//Table header

echo "<table border=0 cellspacing=5 cellpadding=0><tr><td><b>Type</b></td><td><b>Newspaper</b></td><td><b>Circ.</b></td><td><b>City</b></td><td><b>Website</b></td><td><b>E-mail</b></td><td><b>Phone</b></td><td><b>Fax</b></td><td><b>County<b></td><td><b>Member</td><td><b>MCAN</td></tr>";

//Fetch and print all records

while (list ($id, $ntype, $newspaper, $circ, $city, $web, $email, $phone, $fax, $county, $member, $mcan) = mysql_fetch_row($res))  {

// if has website, use it, else use item.php
$url = $web ? 'http://'.$web : "item.php?id=$id";

echo "<tr><td>$ntype</td><td nowrap><a href='$url'>$newspaper</a></td><td nowrap>$circ</td><td nowrap>$city</td><td nowrap>$web</td><td>$email</td><td nowrap>$phone</td><td nowrap>$fax</td><td nowrap>$county</td><td nowrap>$member</td><td>$mcan</td></tr>"; 

}
echo "</table>";
include("links.php");
?>

That part works fine, the name of the newspaper has a link to either the website or the link to item.php. I thought I had a decent understanding of how to make item.php work, but I was wrong:

item.php
<?php

$db = mysql_connect("localhost", "user", "pass" );
mysql_select_db("database",$db);


if (isset($_GET['id'])) {
    if (!is_numeric($_GET['id'])) {
        die('You are trying to use an invalid link');
    }
    else {
        $id = $_GET['id'];
    }
}

$query = "SELECT id, ntype, newspaper, circ, city, web, email, phone, fax, county, member FROM newspapers WHERE id=$id";
$result = mysql_query($query);

echo "<table border=0 cellspacing=0 cellpadding=0>\n";

while ($myrow = mysql_fetch_row($result)) {

//Table header

echo "<table border=0 cellspacing=5 cellpadding=0><tr><td><b>Type</b></td><td><b>Newspaper</b></td><td><b>Circ.</b></td><td><b>City</b></td><td><b>Website</b></td><td><b>E-mail</b></td><td><b>Phone</b></td><td><b>Fax</b></td><td><b>County<b></td><td><b>Member</td><td></tr>";

//Print record

while (list ($id, $ntype, $newspaper, $circ, $city, $web, $email, $phone, $fax, $county, $member) = mysql_fetch_row($res)) 

echo "<tr><td>$ntype</td><td>$newspaper</td><td>$circ</td><td>$city</td><td>$web</td><td>$email</td><td>$phone</td><td>$fax</td><td>$county</td><td>$member</td></tr></table>"; 

?>

Share this post


Link to post
Share on other sites
What isn't it doing that it should do, or conversely, what is it doing that it shouldn't?

Share this post


Link to post
Share on other sites
The index page works fine, and when I click on a newspaper name that has no website (ex. id-3) it returns the correct url:

http://www.mspress.org/newspapers/item.php?id=3

but the page shows the following error:

Parse error: parse error in /home/httpd/vhosts/mspress.org/httpdocs/newspapers/item.php on line 33

Share this post


Link to post
Share on other sites
Parse errors are caused by missing closing quote or missin closing ),] or } or missing ';' at end of line etc. In other words the syntax rules have been broken.

Check line 33 and the few lines above. Line 33 is where it realises there is an error.

Share this post


Link to post
Share on other sites
Your
while ($myrow = mysql_fetch_row($result)) {

had no closing "}"

However, you dont want that line in there anyway. You will only return a single record at most so that line would read it. When you come to the "list () = mysql_feych_row()" you will be at the end of the results set and get nothing.

Try
[code]<?php
$db = mysql_connect("localhost", "user", "pass" );
mysql_select_db("database",$db);


if (isset($_GET['id'])) {
    if (!is_numeric($_GET['id'])) {
        die('You are trying to use an invalid link');
    }
    else {
        $id = $_GET['id'];
    }
}

$query = "SELECT id, ntype, newspaper, circ, city, web, email, phone, fax, county, member FROM newspapers WHERE id=$id";
$result = mysql_query($query);

list ($id, $ntype, $newspaper, $circ, $city, $web, $email, $phone, $fax, $county, $member) = mysql_fetch_row($res); 

echo "<table border=0 cellspacing=0 cellpadding=0>\n";

//Table header

echo "<table border=0 cellspacing=5 cellpadding=0><tr><td>Type</td><td>Newspaper</td><td>Circ.</td><td>City</td><td>Website</td><td>E-mail</td><td>Phone</td><td>Fax</td><td>County</td><td>Member</td><td></tr>";

//Print record



echo "<tr><td>$ntype</td><td>$newspaper</td><td>$circ</td><td>$city</td><td>$web</td><td>$email</td><td>$phone</td><td>$fax</td><td>$county</td><td>$member</td></tr></table>"; 
?>[/code]

Share this post


Link to post
Share on other sites
Your code was perfect, except for one typo I had left in:

$result = mysql_query($query);

should have been

$res = mysql_query($query);

because that's what list calls:

list ($id, $ntype, $newspaper, $circ, $city, $web, $email, $phone, $fax, $county, $member) = mysql_fetch_row($res); 

I used your code, and got an error at line 19 (the list code) and then it was easy to spot.

Thanks again, so much!!!!

Must.get.sleep.

Victoria

Share this post


Link to post
Share on other sites

×

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.