Jump to content


Photo

Please help a newbie!


  • Please log in to reply
13 replies to this topic

#1 vbracknell

vbracknell
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 11 August 2006 - 06:16 PM

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.o...1">newspaper</a
but I am not positive I think thinking along the right lines here.

index.php:
<?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";
?>

item.php:
<?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";
?>

MOD edit: code tags added

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 11 August 2006 - 08:17 PM

Does your "newspapers" table have an "id" column?

Also, does it have a "url" column, with entries for those papers with a site?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#3 vbracknell

vbracknell
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 11 August 2006 - 09:38 PM

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.

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 11 August 2006 - 09:44 PM

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."

If not the url column, which you say you aren't using, how do know a paper doesn't have a site?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 vbracknell

vbracknell
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 11 August 2006 - 09:53 PM

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)  

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 11 August 2006 - 10:34 PM

Store the newspaper name in newspaper and the site address in website (eg www.newyorktimes.com)

Then

<?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>';
?>

EDIT mysql_fetch_array() changed to mysql_fetch_row()
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 vbracknell

vbracknell
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 12 August 2006 - 03:14 AM

Thank you so much! I'll try that!

Victoria

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 12 August 2006 - 08:20 AM

Oops. Change my

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

if you are using my list() mthod.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#9 vbracknell

vbracknell
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 12 August 2006 - 09:27 AM

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>"; 

?>

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 12 August 2006 - 09:34 AM

What isn't it doing that it should do, or conversely, what is it doing that it shouldn't?
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 vbracknell

vbracknell
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 12 August 2006 - 09:41 AM

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.o...s/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

#12 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 12 August 2006 - 09:45 AM

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.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#13 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 12 August 2006 - 09:54 AM

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
<?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>";   
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#14 vbracknell

vbracknell
  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 12 August 2006 - 10:21 AM

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users