Jump to content


Photo

php/mysql question


  • Please log in to reply
8 replies to this topic

#1 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 27 June 2006 - 02:32 AM

So i'm making a clone of digg.com.

This can be located at [a href=\"http://tekkenfan.sytes.net/digg\" target=\"_blank\"]http://tekkenfan.sytes.net/digg[/a]
I am using this while loop to pull all data from the database

while($row = mysql_fetch_array( $result )) {

and then this, to ouptput the data
echo "<table class='sample' valign='top' border='1' cellpadding='5'> <tr> <th width='49' rowspan='2'>";
echo $row['hits'];
echo "<br>";
echo "<u><font size='2'>Hit This!</u></font>";
echo "</th>";

echo "<td width='575'>";
echo "<b>";
?>
<a href="<?php echo $url; ?>"><?php echo $row['title'];?></a>
<?php
echo "</b>";
echo "<br><font size='2'>Article posted on:";
echo $row['date'];
echo "</td> </tr> <tr> <td width='575'>";
echo $row['body'];
echo "</td> </tr> </table>";
echo "<br>";

}


As you can tell, i'm giving a spot for a 'digg this/hit this' link. . .This is of course linked to a db field called 'hits' that i want to use as a 'total' hits that particular story has gotten. And with each click. . .it will add 1 to that certain story's db field.

I assume the way to initiate this process is to make each 'hit this' line, in the table, link to a blah.php page. Having this page do the mysql changes, then maybe give a 'thank you' message, pausing, then doing a header to redirect back to the index.

The issue I come across is how to tell the difference between the stories. How will I go about something like this?

Thanks in advanced for anyone that takes the time to reply.

#2 phpstuck

phpstuck
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 27 June 2006 - 03:47 AM

Each story when entered into the database should have an ID number, use that number to ID each story and also become the ID php uses to update the hit number.

If I am way off base please explain your situation a little better and I'll redo my reply

#3 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 27 June 2006 - 04:45 AM

so how would i go about doing that. I am aware of the id number for each entry into the database. . .but how i do i use it, since i'm just using a while loop to echo the databse.

#4 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 27 June 2006 - 05:29 AM

I am such a noob at everything php and mysql related. What's in my code so far, was learned right when I needed to do that specific task.

Is the above code the blah.php?

if so, how do i pass it along via _session?

Here is my tables code, cause I really dont know what exactly is needed to customize.

// Create a MySQL table in the selected database
mysql_query("CREATE TABLE articles(
id INT NOT NULL AUTO_INCREMENT, 
PRIMARY KEY(id),
 title VARCHAR(255), link VARCHAR(255), category VARCHAR(30), hits INT, date VARCHAR(50), body VARCHAR(5000))"); 

echo "Table Created!";

?>

As much explaination of code is greatly appreciated

#5 phpstuck

phpstuck
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 27 June 2006 - 05:53 AM

Ok I'll try this again, even I was confused by my last post...LOL

Ok on your main page as linked in your first post you need to query the database and assign the id with the hit this link.


$linklist=mysql_query(
        "SELECT id, title, link, category, body FROM articles");

while ($all = mysql_fetch_array($linklist)) {
  $id=$all['id'];
  $title=$all['title'];
  $link=$all['link'];
  $category=$all['category'];
  $body = $all['body'];
  
//then your layout but the hit this link needs to be as follows

 echo "<a href='hit_this_link.php?appid=$id'>[HIT THIS]</a>

// that will attach your id for the GET statment on the processing page

Next you need to create a php page to process the hit this
call it hit_this_link.php

$aprid = $_GET['appid'];

UPDATE articles SET hits = hits + 1 WHERE id='$aprid'");

include_once "the_name of your main page.php";

The rest you'll have to figure out on your own :-)

#6 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 27 June 2006 - 07:07 AM

I use the following to query my database

$result = mysql_query("SELECT * FROM articles ORDER BY id DESC LIMIT ".$start.", ".$limit);


Can I keep it like that. . .instead of how you put it as

$linklist=mysql_query(
        "SELECT id, title, link, category, body FROM articles");

since * pulls everything. . .that would be the same, correct?

i think so. . .but yea. . .it's coming together in my head now. i'll post updates in an hour or so

#7 phpstuck

phpstuck
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 27 June 2006 - 07:16 AM

Yeah you should be fine with that, I was just putting it as simple as I could for example purposes. I'm off to bed but I'll check on you in the morning.. Good luck!

#8 Dville

Dville
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts

Posted 27 June 2006 - 07:21 AM

dood you freaking rock!!!!!

I'm so new, when i first saw the code it was foreign. . .but now that I looked at it for 10 minutes and got back into it. . . .it works!!!!!!!

man I've been putting this off for 5-6 days, being so lost on how I would go about doing this.

if you or anyone could give a detailed explaination, or a link to an article that explains what i just did. . .i would be very greatful. I'd rather learn this than just cut and paste code.

Thanks again!!!!!


EDIT - due to duplicate updates being made, i needed to do a header, insead of include.
but yea, this works fantasticly

#9 phpstuck

phpstuck
  • Members
  • PipPipPip
  • Advanced Member
  • 59 posts

Posted 27 June 2006 - 07:26 AM

The quick explination... You are simply applying the ID number to the exact article when you create the link, that means anything you do with that link will carry the ID number so it applies changes only to that exact article. If you look over it a few times it will begin to become really clear... Glad it worked well for you.

BTW I just checked the page... It all looks really great :-)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users