bindiya Posted March 30, 2011 Share Posted March 30, 2011 I have a php page where I have posts about different topic. I have a menu listing all this categories of topics . Some topics may belong to more than one category. So when i click on a particular menu item, the topic belonging to that category will be displayed. While displaying it is in this format---Topic Heading,Belongs to which all categories,date posted and the description. All this are taken from database. Now my actual issue is I am not able to give links to the categories dispalyed ,if the category exceeds more than 1. For eg: Global Warming Posted on March 11,2011 in News, Issue -----------description of global warming goes here. In the above eg: News and issue are the two categories,when i click on News it should display all the items in news category,and when i click on issue it should display all items of issue category. But I am not able to give this link to the categories. while($row = mysql_fetch_array($q)) { if($row[6]=='News'){ $c='<a href="category_disp.php?category=news">News</a>'; } if($row[6]=='Issues'){ $c='<a href="category_disp.php?category=issues">Issues</a>'; } echo "<tr><td><p >$row[1]</p><br></td></tr>"; echo "<tr><td> <div >". "<p><span>$row[4]</span> <span>". "<a href='$row[6]' >$row[6]|<a href='$row[5]'> Post Comments</a></span></p>". "<p> </p></div><br></td></tr>"; echo "<tr><td><p >$row[2]</p><br></td></tr>"; echo "<tr><td><p ><a href='$row[3]' >Click Here to read more...</a></p></td></tr>"; echo "</div></td></tr>"; } echo" </table> Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 bindiya, First a comment about your code. I highly suggest that you utilize mysql_fetch_assoc() and use the column names in your code rather than the array positions. That code is unreadable for anyone who doesn't have your database schema, and will be very hard to maintain. if ($row[6] == 'News") will read a lot better if the code is: if ($row['category'] == 'News') I am sorry but in order to help you, I need to understand your question. "But I am not able to give this link to the categories" is not a phrase that makes any sense in english. I could not intuit what you are trying to do purely from your code, so perhaps you can try and re-phrase things. Quote Link to comment Share on other sites More sharing options...
bindiya Posted March 30, 2011 Author Share Posted March 30, 2011 I am sorry for not editing the code before post i am posting the code correctly. while($row = mysql_fetch_array($q)) { if($row['category']=='News'){ $c='<a href="category_disp.php?category=news">News</a>'; } if($row['category']=='Issues'){ $c='<a href="category_disp.php?category=issues">Issues</a>'; } echo "<tr><td><p >$row['post_heading']</p><br></td></tr>"; echo "<tr><td> <div >". "<p><span>$row['posted_date']</span> <span>". "<a href='$row[category]' >$row['category']|<a href='$row[comments]'> Post Comments</a></span></p>". "<p> </p></div><br></td></tr>"; echo "<tr><td><p >$row['Posted_By']</p><br></td></tr>"; echo "<tr><td><p ><a href='$row['link_of_post']' >Click Here to read more...</a></p></td></tr>"; echo "</div></td></tr>"; } echo" </table> for example; when i clicked on category News I got many posts which belong to News category.But some among these displayed posts belong to other category also. That is on post can belong to many categories. While displaying the format is Post Heading Date Posted----in Category ----Post comments Description The 'Category' highlighted here contain one,two or more categories in to which the topic is belongs For eg the category in the below example are News and issue. Global Warming Posted on March 11,2011 in News, Issue -----------description of global warming goes here. So when i click on News , the page should display topics that come under News category.To get that I should give the link like <a href="category_disp.php?category=news">News</a>; where category_disp.php is the page where the action takes place. Same way for Issue category , i should provide the link <a href="category_disp.php?category=issues">Issues</a>; This works fine when there is only one category. When the category increases, i am not able to give the links. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 What is the query you are using and what does the result set look like? Quote Link to comment Share on other sites More sharing options...
bindiya Posted March 30, 2011 Author Share Posted March 30, 2011 $post_cat=$_GET['category']; $var=$blog_cat; $q1="select * from post_pages where post_category like \"%$var%\" order by posted_date desc "; $q = mysql_query($q1); if(!$q) die(mysql_error()); $no_row = mysql_num_rows($q); if($no_row == 0){echo "<div name='searched' id='searched'><h4>Results</h4>"; echo "<p class='style26' sizcache='3' sizset='44'>Sorry, your search: "" . $var . "" returned zero results</p></div>"; } else{ while($row = mysql_fetch_array($q)) { if($row['category']=='News'){ $c='<a href="category_disp.php?category=news">News</a>'; } if($row['category']=='Issues'){ $c='<a href="category_disp.php?category=issues">Issues</a>'; } echo "<tr><td><p >$row['post_heading']</p><br></td></tr>"; echo "<tr><td> <div >". "<p><span>$row['posted_date']</span> <span>". "<a href='$row[category]' >$row['category']|<a href='$row[comments]'> Post Comments</a></span></p>". "<p> </p></div><br></td></tr>"; echo "<tr><td><p >$row['Posted_By']</p><br></td></tr>"; echo "<tr><td><p ><a href='$row['link_of_post']' >Click Here to read more...</a></p></td></tr>"; echo "</div></td></tr>"; } echo" </table> Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 I think I finally understand your question. For one of these "multiple category" postings, how do you know that it is part of multiple categories? I need to know how this is handled in the database, and what column that data is represented in. Quote Link to comment Share on other sites More sharing options...
bindiya Posted March 30, 2011 Author Share Posted March 30, 2011 All this is done thru the admin part, there I have multiple checkboxes by which I can select the categories. It is working fine,no issues there. But when it is displayed in the front end, the problm comes Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 That is not what I'm asking. What I'm asking, has to do with the data. I can not guess at the structure of your database table or tables. So far all you provided is: $q1="select * from post_pages where post_category like \"%$var%\" order by posted_date desc "; I can not answer your question when I don't know the structure of post_category, or how the database represents that a post_pages row is in multiple categories. Is this a varchar column that has something like: 'News, Issues' in it? Quote Link to comment Share on other sites More sharing options...
bindiya Posted March 30, 2011 Author Share Posted March 30, 2011 Database table post_head varchar(50),date_posted date,post_category varchar(100),description longtext. Quote Link to comment Share on other sites More sharing options...
gizmola Posted March 30, 2011 Share Posted March 30, 2011 bindiya, You still haven't answered my question about the data, but I'm going to guess that you have the structure i described. Write a function that takes this and generates the series of links you want. Something like this: function make_links($categories) { $links = ''; $categories = explode(',' $categories); foreach($categories as $category) { $category = trim($category); $links .= '' . $category . ''; } return $links; } Call this function in place of where you are generating the link instead. echo make_links($row['category']); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.