Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Closed Thread
Add 1 To $reviewnum When User Clicks Submit?
Old 09-15-2009, 11:39 PM Re: Add 1 To $reviewnum When User Clicks Submit?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Quote:
Originally Posted by Physicsguy View Post
That does!

In my unuploaded version I have a delimeter (the |) so I'm working on making it find that, but so far... No luck...

How would I do this though:
Quote:
You could search for 5 characters, followed by 2 characters, followed by
<div id='contentboxtitle'><h1>but that is cludgy and not subject to easy modification or maintenance.
Because that seems really good! I know it's not easy to change, but I'm not planning on changing it...
Since you're using fopen, you could read each line in and check the first number of characters for a match, echoing out until another match is found once the correct reply has been found.
Quote:
Originally Posted by Physicsguy View Post
Also, as for the Chinese/Spanish symbols, I'm not gonna worry about those because my site is English. Maybe I'll do it after things are working perfectly, but for now I'll stick with the normal characters.
OK, but look for smart quotes to screw things up.
Quote:
Originally Posted by Physicsguy View Post
I'm having some trouble with all this though. Can you please take a look at it, I'm having no luck.
...
You see, I'm having trouble with this. Here's my step checklist:

✓ 1. When you agree to the Terms, it displays the review form.
✓ 2. Allow you to edit or add the review, after it validates.
3. Add the review content (with serial number, and delimeters, the whole thing) to 'ffdatabase.txt', and add a string which contains the serial number, and the URL for that (looking something like "userreview.php?reviewID=$serialNumString") to a database called "serialnumdb.txt".
4. Have a page that pulls all the information from 'serialnumdb.txt', look for the serial number that it has in the page's URL, and then look into 'ffdatabase.txt' for that serial number, followed by the review, and display it.
5. EXTRA, OPTIONAL: Allow the user to edit their own reviews, but not other user's. Again, only if things are running perfectly.

It's tough, it's big, but it's worth it!
Call me plain lazy, but debugging that code with that methodology just seems like doing unnecessary work b/c the script will ultimately break. Now, I know I'm stuck up, but I decided to spend a few minutes and write up a file-based system which will provide a much stronger base from which to work. I'm sure I'll get the regular round of razzing for working for free, but check this out:

WebmasterTalkReview.php (creating a driver object)
PHP Code:
<?php
class WebmasterTalkReview {
  private 
$review_layout '<div class="wt_review"><h1 class="wt_review_title"><a href="#REVIEW_FILE#?review=#REVIEW_ID#">#REVIEW_TITLE#</a></h1><div class="wt_review_body">#REVIEW_BODY#</div><div class="wt_review_rating">Rating: #REVIEW_RATING#</div><div class="wt_review_author">#REVIEW_AUTHOR#</div></div>';
  private 
$target_directory;
  public function 
__construct() {
    
//Set default directory for saving reviews to the current location
    
$this->target_directory dirname(__file__).DIRECTORY_SEPARATOR.'reviews';
    if (!
file_exists($this->target_directory)) {
      
mkdir($this->target_directory);
    }
  }
  public function 
savePostedReview() {
    if (empty(
$_POST)) return;

    
$title $this->sanitizeData($_POST['review_title']);
    
$body $this->sanitizeData($_POST['review_body']);
    
$rating = (int)$_POST['review_rating'];
    
$author $this->sanitizeData($_POST['review_author']);
    
$review_date date('n/j/y');
    if (
strlen($title) > && strlen($body) > && strlen($rating) > && strlen($author) > 0) {
      
$file_contents serialize(array('title'=>$title,
                                       
'body'=>$body,
                                       
'rating'=>$rating,
                                       
'author'=>$author,
                                       
'date'=>$date
                                      
)
                                );
      
$file_name md5(uniqid(mt_rand(), true)).'.txt';
      
$fh = @fopen($this->target_directory.DIRECTORY_SEPARATOR.$file_name'w');
      if (
flock($fhLOCK_EX)) {
        
fwrite($fh$file_contents);
        
flock($fhLOCK_UN);
      } else {
        
//Hopefully people don't get here, but let's handle it anyway.
        
throw new Exception('File is currently in use.  Please try re-submitting');
      }
      
fclose($fh);
    } else {
      throw new 
Exception('Please fill in all fields');
    }
  }
  public function 
showCurrentReviews() {
    if (
file_exists($this->target_directory)) {
      foreach (
glob($this->target_directory.DIRECTORY_SEPARATOR.'*.txt') as $file_name) {
        
$a_review unserialize(file_get_contents($file_name));
        
$a_review_w_layout str_replace(array('#REVIEW_TITLE#',
                                               
'#REVIEW_BODY#',
                                               
'#REVIEW_RATING#',
                                               
'#REVIEW_AUTHOR#',
                                               
'#REVIEW_DATE#',
                                               
'#REVIEW_FILE#',
                                               
'#REVIEW_ID#'
                                              
),
                                         array(
$a_review['title'],
                                               
$a_review['body'],
                                               
$a_review['rating'],
                                               
$a_review['author'],
                                               
$a_review['review_date'],
                                               
basename($_SERVER['PHP_SELF']),
                                               
basename($file_name,'.txt')
                                              ),
                                         
$this->review_layout
                                        
);
        echo 
$a_review_w_layout;
      }
    }
  }
  public function 
showReviewInputForm() {
    
$title $this->sanitizeData($_POST['review_title']);
    
$body $this->sanitizeData($_POST['review_body']);
    
$rating = (int)$_POST['review_rating'];
    
$author $this->sanitizeData($_POST['review_author']);

    echo 
'<form method="POST" action="'.basename($_SERVER['PHP_SELF']).'" class="review_form">',
         
'<label>Title:<input type="text" name="review_title" value="'.$title.'"/></label>',
         
'<label>Your Review:<textarea name="review_body">'.$body.'</textarea></label>',
         
'<label>Rating:<select name="review_rating">
  <option value="1"'
.($rating == 1?' selected':'').'>1</option>
  <option value="2"'
.($rating == 2?' selected':'').'>2</option>
  <option value="3"'
.($rating == 3?' selected':'').'>3</option>
  <option value="4"'
.($rating == 4?' selected':'').'>4</option>
  <option value="5"'
.($rating == 5?' selected':'').'>5</option>
</select></label>'
,
         
'<label>Name:<input type="text" name="review_author" value="'.$author.'"/></label>',
         
'<input type="submit" value="Submit Review" />',
         
'</form>';
  }
  public function 
showReview($review_file_name) {
    
$review_file_name preg_replace('/[^0-9a-f]/','',$review_file_name);
    if (
file_exists($this->target_directory.DIRECTORY_SEPARATOR.$review_file_name.'.txt')) {
      
$a_review unserialize(file_get_contents($this->target_directory.DIRECTORY_SEPARATOR.$review_file_name.'.txt'));
      
$a_review_w_layout str_replace(array('#REVIEW_TITLE#',
                                             
'#REVIEW_BODY#',
                                             
'#REVIEW_RATING#',
                                             
'#REVIEW_AUTHOR#',
                                             
'#REVIEW_DATE#',
                                             
'#REVIEW_FILE#',
                                             
'#REVIEW_ID#'
                                            
),
                                       array(
$a_review['title'],
                                             
$a_review['body'],
                                             
$a_review['rating'],
                                             
$a_review['author'],
                                             
$a_review['review_date'],
                                             
basename($_SERVER['PHP_SELF']),
                                             
substr(basename($file_name),0,-4)
                                            ),
                                       
$this->review_layout
                                      
);
      echo 
$a_review_w_layout;
    }
  }
  public function 
__get($variable_name) {
    switch (
$variable_name) {
      case 
'avg_rating':
        
$sum $count 0;
        if (
file_exists($this->target_directory)) {
          foreach (
glob($this->target_directory.DIRECTORY_SEPARATOR.'*.txt') as $file_name) {
            
$a_review unserialize(file_get_contents($file_name));
            
$sum += $a_review['rating'];
            
$count++;
          }
        }
        if (
$count 0) {
          return 
$sum/$count;
        } else {
          return 
0;
        }
        break;
    }
  }
  public function 
__set($variable_name$variable_value) {
    switch (
$variable_name) {
      case 
'review_layout':
        
$this->review_layout $variable_value;
        break;
      case 
'target_directory':
        if (
file_exists($variable_value)) {
          
$this->target_directory $variable_value;
        }
        break;
    }
  }
  private function 
sanitizeData($dirty) {
    
$clean $dirty;
    
$clean htmlspecialchars($clean);
    return 
$clean;
  }
}
?>
index.php (example usage script)
PHP Code:
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <title>Example Reviews Stored As Files</title>

    <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"><!-- Makes HTML 5 elements available for styling --></script>
    <style type="text/css">
      .review_form {
        border:2px #090 inset;
        background:#afa;
        width:35em;
        padding:.5em;
      }
      .review_form label {
        display:block;
        margin-bottom:.5em;
      }
      .review_form textarea {
        display:block;
        width:25em;
        height:8em;
      }
      .review_form input, .review_form select {
        margin-left:1em;
      }
      .review_form input[type=submit] {
        border:2px #0F0 outset;
        background:#cfc;
        cursor:pointer;
      }
      .review_form input[type=submit]:hover {
        border:2px #0F0 inset;
      }
      .error_message {
        width:36em;
        background:#900;
        color:#fff;
        font-weight:bold;
        text-align:center;
      }
      .wt_review {
        border:1px #060 solid;
        margin:5px 0;
        width:35em;
        padding:.5em;
      }
      .wt_review_title {
        margin:0;
        border-bottom:1px #030 dashed;
      }
      .wt_review_body {
        font-family:Courier New, Courier, monospace;
        margin:.5em 0;
        font-size:90%;
      }
      .wt_review_rating {
        font-weight:bold;
        font-size:95%;
      }
      .wt_review_author {
        font-style:italic;
        font-size:110%;
      }
    </style>
  </head>
  <body>
    <section id="main_section">
      <?php
require_once('WebmasterTalkReview.php');
$my_reviews = new WebmasterTalkReview();
echo 
'<div id="avg_rating">OUR AVERAGE RATING IS '.$my_reviews->avg_rating.'</div>';
if (
strlen($_GET['review']) > 0) {
  
$my_reviews->showReview($_GET['review']);
} else {
  try {
    
$my_reviews->savePostedReview();
  } catch (
Exception $e) {
      echo 
'<div class="error_message">'.$e->getMessage().'</div>';
  }
  
$my_reviews->showCurrentReviews();
}
$my_reviews->showReviewInputForm();
?>
    </section>
  </body>
</html>
both files in the same directory should "just work". Sorry not to answer directly.

Quote:
Originally Posted by Physicsguy View Post
And JM, I was just looking at your TeraTask site - nice - but I notice that you are doing the same thing I want to do. All your pages are "index.php?page=SOMETHING" which is what I want except "userreview.php?reviewID=$serialNumString"! No wonder you understand, you have the same kind of thing! Nice. :O
You may want to check my post at http://www.webmaster-talk.com/showth...229#post898229 . There, I provide a detailed site-creation methodology and provide files which could be adjusted.

I hope this gets you on your way.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
View Public Profile Visit JeremyMiller's homepage!
 
 
Register now for full access!
Old 09-16-2009, 01:39 AM Re: Add 1 To $reviewnum When User Clicks Submit?
bmcoll3278's Avatar
Super Talker

Posts: 118
Name: Brian Collins
Trades: 0
Quote:
Originally Posted by JeremyMiller View Post
  1. First, I'd say to change your TK policy. Giving thanks shouldn't be to the "winner", but to all those who provided some guidance that was informative or helpful and encourages people to proffer their ideas. If everyone followed the winner/most-helpful policy, then many who are not as skilled/informed/whatever would not try to help, not wanting to compete against those who they perceive as more-qualified, reducing the overall pool of ideas. Even the least educated, least experienced person can come up with a brilliant way of solving a problem.
  2. I have posted to your form with some test data. You'll see these issues:
    1. Foreign characters (both Chinese and Spanish) did not display correctly on your code-dump or preview pages.
    2. I inserted code which was properly handled in the preview pages, but you'll want to determine your preferred means of handling such code.
    3. HTML entities, such as the euro symbol (€), whether entered directly or by the entity code itself, did not render correctly
    4. There does not appear to be a delimiter between posts and you allow newlines to be entered. This will mean that searching for a specific review will be challenging. You could search for 5 characters, followed by 2 characters, followed by
      <div id='contentboxtitle'><h1>but that is cludgy and not subject to easy modification or maintenance.
Hope that helps.
Thank you. I have got a lot of help here and I try to give back but I am a noob compared to a lot of the guys here. On another form I use I am a goto guy for php as you can guess the other users are not php gruroo either.
But what you said is very nice to hear!!
__________________
I hope to build a site with something for every one

Please login or register to view this content. Registration is FREE
bmcoll3278 is offline
View Public Profile Visit bmcoll3278's homepage!
 
Old 09-16-2009, 05:08 PM Re: Add 1 To $reviewnum When User Clicks Submit?
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 824
Name: Scott
Location: Ontario
Trades: 0
Holy crap Jeremy Miller, that's insane, thank you!

Great job, but I have so say this:

I want to write the script. It's amazingly awesome that you can easily type up a working database puller and scripter, and I'd user it any day, but I want to be able to figure it out on my own, with a little help. What you've done is you've done it for me, like a Grade 12 helping a Grade 9, the Grade 9 doesn't learn anything, except that the answer is right.

I want to learn how to do this myself, and I already have some great ideas on how, but I am actually planning on releasing this to the public as web software, and I really don't want to be releasing something that I have no idea how it works!

I've looked at your code, trying to find the database-puller from a certain number script, but I can't find it, the stuff you've written is way over my head (that's good)! All I really need is a way for the userreview.php to pull a certain spot from a database.

I need a way for the database to search for a line beginning with $serialNumString, and then pull the number along with it's contents out into the page.

I can easily pull everything in the database into the page, have it display all of it at once, and maybe I can do that, and have only part of it display, but I don't want the database to be adding extra PHP code that the page doesn't need, because that would be a waste of code, time, and memory.

Here's what I don't know how to do, and I need to know.
How to 'search' for something in a database, and display it. I'm gonna start small and work my way up, so this is my goal for now.
__________________
Check out my
Please login or register to view this content. Registration is FREE
Physicsguy is online now
View Public Profile
 
Old 09-16-2009, 05:19 PM Re: Add 1 To $reviewnum When User Clicks Submit?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
You accuse me of being nicer than I am -- thanks. All I did was provide a different structure that solves a lot of your major issues. There is still much to be done.

Because of the structure of your methodology, helping you on that is like trying to build something without having power tools usually used for construction: more of a headache than a help. And, at the end, your implementation won't be as robust or easily modifiable.

There are only a few methods to my object and they take care of the basic tasks you'll need, but are much easier to work with and modify (for me, perhaps someone else will disagree) and even demo how to search your flat-file database as well as display a review.

I provided that code as a teaching methodology: to show you grade 12 work that you may be able to strengthen your skills a few notches. Things are named quite well and demo all available functionality, giving you a much stronger base from which to build your site with much less effort than you've already exerted.

Analogously, this is like an author writing a book and another author coming through and saying, "Nice try, but read this other book and see how you can strengthen your writing."

As for which function shows a post, see the showReview function -- that's what it's intended to do.

And, as for releasing your code, feel free to steal mine -- I released it here for everyone's free usage.

I hope this helps you on your way to achieving your goals and bettering your skills.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
View Public Profile Visit JeremyMiller's homepage!
 
Old 09-16-2009, 07:01 PM Re: Add 1 To $reviewnum When User Clicks Submit?
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 824
Name: Scott
Location: Ontario
Trades: 0
Why thank you!

I understand that I may not know everything or as much as you do, but I haven't had the time for the experience that you have from your years of coding, I've only been learning PHP since July, now, so I'm pretty new to this whole thing.

I also understand that yours is better than mine, and mine is going to be hard to modify. That hurts, I'm trying to make something I'm extremely proud of, and when you write the same thing with a much smaller code, it kind of insults me, in a way. Don't get me wrong - I still like you and your code, but I don't really like being put off, and being just handed a better, working code.

As I said, over time I will improve the code, making it smaller, more customizable, and I am already planning on putting in a simple configuration file to help set the numbers and such, but only when I'm done, because it'll be a pain in the butt to change it back for one added feature.

So, thanks again, I'll take a look at showReview, and I just want to say THANKS FOR EVERYTHING!!!

-- EDIT --

So, I need this from your code:

PHP Code:
require_once('WebmasterTalkReview.php');
$my_reviews = new WebmasterTalkReview();
if (
strlen($_GET['review']) > 0) {
  
$my_reviews->showReview($_GET['review']);

Which I could adapt to:
PHP Code:
require_once('process.php');
$my_reviews = new Review();
if (
strlen($_GET['reviewfull']) > 0) {
  
$my_reviews->showReview($_GET['reviewfull']);

??

I don't get it, so small of a code displays the entire review?

I have it saved and all (in the database) with a serial number next to it, I need a way for the script to recognize that number and pull it and the review that corresponds with the number to the page, where it displays.

Currently, this is the important part of 'userreview.php'

Code:
<?php
$fp = fopen('serialnumdb.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}

while (!feof($fp)) {
    $line = fgets($fp,1024); //use 2048 if very long lines
    list ($field1) = split ('\|', $line);
    
echo "$field1.";
    $fp++;
}

fclose($fp);
?>
<?php
$fp = fopen('ffdatabase.txt','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}

while (!feof($fp)) {
    $line = fgets($fp,1024); //use 2048 if very long lines
    list ($field1) = split ('\|', $line);
    
echo "$field1.";
    $fp++;
}

fclose($fp);
?>
And here's a sample output of the database with 2 reviews inputted into it:

Code:
WyLkV | <div id=\"contentboxtitle\"><h1>TITLE</h1></div><div id=\"contentbox\"><em>A review by AUTHOR</em><br /><br />REVIEW
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
</div><div id=\"contentboxtitle\"><h3>Rating: 1/10</h3></div><div id=\"contentbox\">REASON fillertext</div> |

h2fLr | <div id=\"contentboxtitle\"><h1>TITLE2</h1></div><div id=\"contentbox\"><em>A review by AUTHOR2</em><br /><br />REVIEW2
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
fillertext
</div><div id=\"contentboxtitle\"><h3>Rating: 1/10</h3></div><div id=\"contentbox\">REASON fillertext2</div> |
You see, it just contains enough data to output the content part of the review. Sorry about the massive 'fillertext' but I had to make enough characters to let it validate...

Notice how the first review has the serial code 'WyLkV' and the second 'h2fLr'. I need a script that will find the line that starts with one of those serial numbers, and then display all the data from there to the delimeter (|). I don't care if the serial number shows up in the review, I can easily fix that.

Thanks, and I know I'm being a pain by not wanting to use your code, but I hope you understand me when I say that I want to understand what I'm releasing... :P

I know I can learn from it, but it's a little too much at once for me, I can only take genius in small doses :P
__________________
Check out my
Please login or register to view this content. Registration is FREE

Last edited by Physicsguy; 09-16-2009 at 07:16 PM..
Physicsguy is online now
View Public Profile
 
Old 09-16-2009, 07:05 PM Re: Add 1 To $reviewnum When User Clicks Submit?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Oh, it wasn't meant to insult you -- we can all use improvement. I personally love when people suggest better code (or solutions, generally speaking) as it provides an opportunity for me to learn.

Best wishes on great success with this!
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
View Public Profile Visit JeremyMiller's homepage!
 
Old 09-16-2009, 07:18 PM Re: Add 1 To $reviewnum When User Clicks Submit?
Physicsguy's Avatar
404 - Title not found

Latest Blog Post:
Challenges
Posts: 824
Name: Scott
Location: Ontario
Trades: 0
No, no, no, don't worry about it, I'm glad you posted that, it's given me some great new ideas, like the rating average, and letting other's rate the review!1

This is great! Also, please read the above post, as I edited it to have more information in it... Thanks again so much, you're at the top of the list in the 'special thanks to...' list

-- EDIT --

Maybe I'll try this;

http://www.designdetector.com/demos/...-3.php?line=11

!!
__________________
Check out my
Please login or register to view this content. Registration is FREE

Last edited by Physicsguy; 09-16-2009 at 08:20 PM..
Physicsguy is online now
View Public Profile
 
Closed Thread     « Reply to Add 1 To $reviewnum When User Clicks Submit?

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28656 seconds with 11 queries