Categorize my blog topic...
Posted: Mon Nov 07, 2011 6:32 pm
hello jacek in your blog tutorial i want to categorize to arranged the topic...i want a simple idea to start my coding...
function submit_category($category){ $category = mysql_real_escape_string(htmlentities($category)); mysql_query("INSERT INTO `blog_cat` (`name`) VALUES ('{$category}')"); } if(isset($_POST['category'])){ submit_category($_POST['category']); header('Location: index.php'); die(); } <table border=0> <form action="" method="post"> <tr> <td>Category Name</td><td><input type="text" name="category"></td></tr> <td align='right' colspan=2><input type="submit" name="Submit Category"></td> </table>
Then in the posts table you'd have a "cat_id" or similar column which corresponds to the id of the category that the post is posted in.louiegiezer wrote:I finished the database structure...now i make the form of add a category...function submit_category($category){ $category = mysql_real_escape_string(htmlentities($category)); mysql_query("INSERT INTO `blog_cat` (`name`) VALUES ('{$category}')"); } if(isset($_POST['category'])){ submit_category($_POST['category']); header('Location: index.php'); die(); } <table border=0> <form action="" method="post"> <tr> <td>Category Name</td><td><input type="text" name="category"></td></tr> <td align='right' colspan=2><input type="submit" name="Submit Category"></td> </table>
everything's ok....so whats the next
That sounds like a good place to start. Take it step by step, first just try to make a function to get the categories then worry about the html and submitting. It should be possible, as it should just be a simple SELECT query.louiegiezer wrote:I have a plan to make an select option tag to call my categories in the table of blog_cat and insert into the database of posts. how can i start work in this part...haha
<p class="topic-cat"><select name='cid'><option value='0'>Please Choose</option> <?php $query = "SELECT * FROM `blog_cat` ORDER BY id ASC"; $result = mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result) == 0){ echo "</select><br>No categories exist"; }else { while($row = mysql_fetch_assoc($result)){ echo "<option value=\"".$row['id']."\">".$row['name']."</option>\n"; } } echo "</select>"; ?></p>and for the function i add $cid on the function of posts to insert it on the database...
<div id="categories"> <?php $select = mysql_query("SELECT * FROM `blog_cat`"); while($data = mysql_fetch_assoc($select)){ echo"<label>$data[name]</label><br>"; $select2 = mysql_query("SELECT * FROM `posts` WHERE `post_cid`='".$data['id']."' "); while($data2 = mysql_fetch_assoc($select2)){ echo "$data2[post_title]<br>"; } } ?> </div><!--Categories-->looks perfect for the code going back to the html...if i click the category it will show the topic list ...i dont know if it is a hover or a drop down tag...haha
$sql = "SELECT `blog_category`.`id`, `blog_category`.`cat_name`, `posts`.`post_title` FROM `blog_category` INNER JOIN (`posts`) ON `blog_category`.`id`=`posts`.`post_category_id`"; $rows = mysql_query($sql); while ($data = mysql_fetch_assoc($rows)){ echo "$data[cat_name]<br>"; echo "$data[post_title]<br>"; }
$sql = "SELECT `blog_category`.`id`, `blog_category`.`cat_name`, `posts`.`post_title` FROM `blog_category` INNER JOIN `posts` ON `blog_category`.`id` = `posts`.`post_category_id`"; $rows = mysql_query($sql); $posts = array(); while ($data = mysql_fetch_assoc($rows)){ $posts[$data['cat_name']][] = array( 'id' => $data['id'], 'title' => $data['post_title'], ); } return $posts;
$posts = get_categories(); foreach ($posts as $get){ ?> <label><?php echo $get['name']; ?></label> <label><?php echo $get['post_title']; ?></label> <?php } ?>posts.php
function get_categories(){ $sql = "SELECT `blog_category`.`id`, `blog_category`.`cat_name`, `posts`.`post_title` FROM `blog_category` INNER JOIN `posts` ON `blog_catagory`.`id` = `posts`.`post_category_id`"; $row = mysql_query($sql); $posts = array(); while ($data = mysql_fetch_assoc($row)){ $posts[$data['cat_name']][] = array( 'id' => $data['id'], 'title' => $data['post_title'], ); } return $posts; }
echo '<pre>', print_r($posts), '</pre>';and you should see where you have gone wrong.
what ?louiegiezer wrote:thanks i still confused in array...