Jump to content

Several Theory PHP Questions


Bopo

Recommended Posts

Hi

 

Basically I'm having to create a simple blog, which has most of the typical features of blogs, I've made good progress, however I have a few questions before I venture any futher that I would like input on.

 

First, blog post are always displayed in reverse chronological order, newest first, I'm using a mysql table with the date, an unique id, author, and post fields. I was thinking of running an sql query to select the records via ascending date and limit it to just 4 records, therefore the page doesn't become cluttered, my real question is how would I separate the records, so I could display them in different parts of a website layout, so they are not all bunched together, like:

 

<div box>

1strecord

</div>

</br>

</br>

<div second box>

2nd record

</div> etc

 

Secondly, I want allow the user to create categories for specific blog posts, I'm assuming you use some php to create a new kinda on index page page called for example 'lifestyle', and then all the lifestyle pages would appear on that page.

 

Finally, I really have no idea about generating links/pages to display content once it has been stored within the database, so any links to guides about this would be appreciated.

 

Thanks for reading.

Link to comment
https://forums.phpfreaks.com/topic/150569-several-theory-php-questions/
Share on other sites

1) You would need another column in your table to seperate the content. e.g. create a table with a column name 'catagory'. Then call the lists with this as a clause.

 

2)To allow users to create their own catagories, you just create an extra <input> field for them to name their catagory.

Then to display the link you would need to run through a loop to get all the available catagories and assign each to the same page but using a GET value.

e.g.

Lets say I want a catagory called 'cars'.

I enter this, it gets put into your database and in the catagory column goes 'cars'.

Then to echo out the links use a loop:

 

while($row = $sql_query->fetchObject())

{

  echo '<a href="www.my_catagory_page.php?catagory='.$row->catagory.'">'.$row->catagory.'</a>';

echo '<br />';

}

 

Then when a user clicks this link they will get taken to:

www.my_catagory_page.php?catagory=cars

 

Then in you script for your my_catagory_page.php  you need to do somthing like this:

 

$catagory = $_GET['catagory'];

 

$result = $conn->query("SELECT * FROM table WHERE catagory = '$catagory'");

$data = $result->fetchObject();

 

$id = $data->id;

$author = $data->author;

$post = $data->post;

$catagory = $data->catagory;

 

Then you can echo these out like:

 

echo 'The id of this post is: '.$id.'<br />';

echo 'The author of this post is: '.$author.'<br />';

echo 'The message of this post is: '.$post.'<br />';

echo 'The catagory of this post is: '.$catagory.'<br />';

 

Hope this is of help.

Hi

 

Thanks for the insightful post, it has helped alot, so I have followed your advice, and create a page name categories as in your example and used the following code

 

<?php

include('blogconnect.php');

while($row = $sql_query->fetchObject())
{
  echo '<a href="www.my_catagory_page.php?catagory='.$row->catagory.'">'.$row->catagory.'</a>';
echo '<br />';
}

?>

 

I'm quite sure the row and sql_query variables need to be set for this to work, the thing it I don't know what kinda of mysql I require, and I don't really know what to do with $row.

 

I will be reading up in the mean time until I get a responce

 

Thanks again.

When you make a query (and it's successful) it'll return a single row from the table.

 

Imagine a spreadsheet in Excel. First row A will be returned, then row B and so-on.

 

So some code like this:

$sql="SELECT * FROM blog ORDER BY id LIMIT 0,4 DESC";
$query=mysql_query($sql);
while ($row=mysql_fetch_assoc($query)) {
  echo 'CHUNK OF DATA:<br />';
  echo $row['id'].'<br />';
  echo $row['author'].'<br />';
  echo $row['post'].'<br />';
  echo $row['category'].'<br />';
}

That will return something like this:

CHUNK OF DATA:
4
fred willis
owner
human
3
spot
pet
dog
2
charlie willis
son
human
1
polly
pet
bird

 

The "LIMIT 0,4 DESC" shows the last 4 in reverse order.

 

That what you mean?

yeah u need to use the last guys post instead of the functions I gave you such as mysql_fetch_assoc($query);

Because the ones I use are if you are using PDO with php, so sorry about that confusion.

The idea of $row is to find the row in your table and find the data it holds in each column. Its basically a handle.

$row->data  = means 'go and get me this 'data'' then when u echo it it will show the value of that column. I wont go on to much i think i am just confusing the situation...I have it in my head but cant explain it its late...

Yeah that's what I was looking for, I had to modify the query a little to get it working, below is the working version for future users

 

$sql = "SELECT * FROM blogposts ORDER BY id DESC LIMIT 0, 3;";

 

Now the tricky bit is, generating links for the categories and a 'more' link for each of the articles, as it wouldn't be practical to show all the content for each blog post on 1 page.

 

If anyone has a clue on how to do this, I'd appreciate input :).

Well to select a few of the words there is a MYSQL function for this:

 

SUBSTRING_INDEX(post,' ', 4) AS words

 

Now just select this and output it, that should get you the first 4 words from 'post'.

 

I have actually shown you how to create links for the catagories, unless im not understanding you right...

Archived

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

×
×
  • 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.