Jump to content
Larry Ullman's Book Forums

Ghamdan

Members
  • Posts

    55
  • Joined

  • Last visited

Everything posted by Ghamdan

  1. I am trying to access a variable inside a while loop to echo a message. php passes me an error message saying " undefined var on ..... and ....." How can I implement that when I am using a while loop? Thank you.
  2. function redirct_none_admin($check ='user_id', $page='index.php'){ if(!isset($_SESSION[$check])) { $url = BASE_URL . $page; header("Location: $url"); exit(); } elseif($_SESSION['user_type'] != 'admin') { $url = BASE_URL . $page; header("Location: $url"); exit(); } } ======================================== It is working but I do not know if it is the right way to do it.
  3. I want to redirect only non-admin users. Admin can access the page only. This is what I am trying to do.
  4. I am trying to create a script that redirect the none admin users. The code below: session_start(); function redirct_none_admin($check ='user_id', $target='index.php'){ if(!isset($_SESSION[$check])) { $url = BASE_URL . $target; header("Location: $url"); exit(); } } function call: // Redirect non-administrators: redirct_none_admin('user_admin'); I could not redirect the none-admin user. Is there anything wrong with this code? How to fix it? Thank you.
  5. I have read your post : Enabling Url Rewriting Not Working httpd.conf : LoadModule rewrite_module modules/mod_rewrite.co the htaccess file is: .htaccess But it is still the same. Thank you.
  6. I am trying to use mode_rewrite in my project but it is not working. httpd.conf Settings: <Directory "C:/xampp/htdocs/mysite/blog"> AllowOverride All </Directory> .htaccess Settings: <IfModule mod_rewrite.co> RewriteEngine on RewriteBase /mysite/blog/ # For article: RewriteRule ^article/([0-9]+)/?$ article.php?id=$1 </IfModule> How can I fix this problem? Thank you.
  7. What I am trying to do is ORDER BY dated_added not views. But the problem is the list is order by active views, means the last post viewed goes first in list which I do not want to. Thank you .
  8. Thank you. The query for page which lists the posts: $q = "SELECT post_id, views ,author, title, content, DATE_FORMAT(date_added, '%M %d, %Y') AS posted FROM posts ORDER BY date_added DESC "; Single post: // views counter: $q = 'UPDATE posts SET `views` = `views`+1 WHERE post_id='.$_GET['id']; $r = mysqli_query($dbc, $q); $q = 'SELECT views, author, title content, DATE_FORMAT(date_added, "%M %d, %Y") AS posted FROM posts WHERE post_id=' . $_GET['id'];
  9. // views counter: $q = 'UPDATE posts SET `views` = `views`+1 WHERE post_id='.$_GET['id']; $r = mysqli_query($dbc, $q); This code works fine and I have noticed that when the page is viewed the order of posts in the main page (view_posts.php) changes according to the last post viewed. Although mysql query sets the order by to date_added DESC. The problem is that the last post viewed or displayed goes first in list. How can I fix this issue ? Thank you.
  10. I want to display some related posts when a post is viewed. For example if the post is about windwos 8 then some related posts to windows 8 will be displayed and so on. This is main idea. How can I implement this feature using php and mysql? Thank you.
  11. I have got a posts table with columns of title, author and content. Indeed I want to retrieve 3 post titles related to the one is viewed at the end of the post. The question is: How can I retrieve related posts using php and mysql? Thank you.
  12. Thank you, Edward. I have added it at the beginning of the script before the main script to view the post.
  13. Counting the page views. I am using this code: // views counter: $q = 'UPDATE posts SET `views` = `views`+1 WHERE post_id='.$_GET['id']; $r = mysqli_query($dbc, $q); to count the page views. But I do not know if it is wright, although it is working. What do you recommend me to do? Thank you.
  14. Sorry Antonio. I really got stuck. There is column named views I have done a separate queries as you can see below <article> <h1>Most popular post </h1> <?php // Get value from GET array $p_id = intval($_GET['id']); // This will be post_id 10 if you use the link above // Set queries $q = "SELECT * FROM testposts WHERE post_id = {$p_id} LIMIT 1"; $q .= "UPDATE testposts SET views=(views+1) WHERE post_id = {$p_id}"; // Notice .= here. This means it will be added to first string // run both queries $r = @mysqli_multi_query($dbc, $q); $q = "SELECT post_id, title, views FROM testposts ORDER BY views DESC LIMIT 4"; $r = @mysqli_query($dbc, $q); // If it ran OK : if($r) { while($row = mysqli_fetch_array( $r, MYSQLI_ASSOC )) { echo ' <p> <a href="article.php?id='. $row['post_id']. '"> Latest: ' . $row['title']. ' </a> </p>'; } } else { echo 'There is no post at the moment!'; } ?> </article> Thank you for trying to help me.
  15. Thank you Antonio, I have done the following but it is not working. <?php // Counting the views: $p_id = intval($_GET['id']);// post id variable. $q = "SELECT post_id, title FROM posts WHERE post_id ={$p_id} LIMIT 5"; $q .= "UPDATE posts SET views=(views+1) WHERE post_id = {$p_id}"; // run both queries $r = @mysqli_multi_query($dbc, $q); // If it ran OK : if($r) { while($row = mysqli_fetch_array( $r, MYSQLI_ASSOC )) { echo ' <p> <a href="article.php?id='. $row['post_id']. '"> ' . $row['title']. ' </a> </p>'; } } else { echo 'There is no post at the moment!'; } ?>
  16. I am trying to retrieve the most viewed post (popular) but it is not working. Is this syntax right? // Retrieve the most viewed post $q = " SELECT post_id ,title COUNT(title) AS most FROM posts GROUP BY title ORDER BY most DESC LIMIT 5"; $r = @mysqli_query($dbc, $q); if($r) { while($row = mysqli_fetch_array( $r, MYSQLI_ASSOC )) { echo ' <p><a href="article.php?id='. $row['post_id']. '"> ' . $row['most']. ' </a> </p>'; } =========================================== Thank you
  17. I have done some changes as follows: php:mb_substr($row['content'], 0, 300, 'UTF-8' ) mysql: SUBSTRING( content, 1,300) Thank you everyone and the great author Larry.
  18. I am using this one: MySQL: SUBSTRING( content, 501) AS content, PHP: <p> Article: ' . substr($row['content'], 0, 500) . ' ...' . ' </p> Thank you
×
×
  • Create New...