Jump to content

Archived

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

Dville

php/mysql question

Recommended Posts

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

[code]while($row = mysql_fetch_array( $result )) {[/code]

and then this, to ouptput the data
[code]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>";

}[/code]


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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

[code]// 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!";

?>[/code]

As much explaination of code is greatly appreciated

Share this post


Link to post
Share on other sites
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.

[code]

$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
[/code]

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

[code]
$aprid = $_GET['appid'];

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

include_once "the_name of your main page.php";
[/code]

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

Share this post


Link to post
Share on other sites
I use the following to query my database

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


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

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

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

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 :-)

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.