Jump to content

TOA

Members
  • Posts

    623
  • Joined

  • Last visited

Posts posted by TOA

  1. What you're describing is the basic functionality of a Learning Management System. They have content authoring programs that can embed quizzes and everything. The two I can think of off the top of my head are Lectura and Articulate. (Note, I have used Articulate in the past, but it's been a few years so their offerings may have changed.) If I remember correctly, you can host/run them from your own site. Don't re-invent the wheel if you don't have to.

     

    Hope that helps. 

  2. Wordpress provides this to you in the loop as the_id() I think, but double check that.

     

    But to answer your question, here's how you'd do it:

    set meta_value = '5' where meta_key = '_price' AND post_id=(select post_id from postmeta where meta_key = '_sku' and meta_value = 'GF-1370')

     

    But use the post_id wordpress gives you, it's a much cleaner query

  3.  

    Ok I've literally been at this for about  2 hours. After some tweaks I've managed to get this error:

     

    Fatal error: Call to a member function getName() on a non-object in C:\xampp\htdocs\tutorials\basiccomment\index.php on line 34

     

    This is because of this code: $comment->author->getName() and $comment->getContent().

     

    If I change to this:

     

    $comment->author & $comment->comment

     

    Then I don't get the above "fatal error" instead it posts empty "name" and "commentcontent". If fact it posts ":;" on the html file.

     

    I think I'm starting to understand the theory and the logic behind it but I've not got it to work so I still haven't fully grasped it. I've been switching between recommended links, sites and tutorials but they do things slightly different from each other so it's hard to fit it into a real world application. I know I've done it wrong because it's not posting anything but I'm not sure what I need to do to put it right. I've been doing some changes here and there but I've still not got it to work. I think I missing a function somewhere but I'll post the code in full if anyone is kind enough to offer me any information.

     

    You're getting that error because the getName function is not defined in the user class. Sorry, I should have been clearer about that. You'll need to define that method and give it the needed functionality to return the author's name. And for what it's worth, author is a better name than user, it's clearer. 

     

    You should really step back and learn the basics though. It seems like you might be getting ahead of yourself.

  4. Well besides the posts by ignace and KevinM1, which gave you alot of what you needed, Symphony has a pretty good article about moving from procedural to OOP, but I have to postface it with: OOP requires a different kind of thinking, so this will only get you so far into the OOP world. This is just a good place to get your feet wet.

     

    That being said, my first comment is: is a user a type of comment? Not in my mind, so you'd need a User object to pass into your comment object (dependency injection) which then gets assigned to the private $author property. Does that make sense?

    Always ask yourself 'is this a type of that' and if the answer is No, then it is a good place for a new class. So it would look like this

    $User = new User;
    $comment = new Comment($User);
    
    // now to get the author of the comment, we'd
    echo $comment->author; 
    // although this would cause an error because the property is private. We'd need to change it to public or create getters/setters

    Now to expand on that, we can convert your procedural code to be like this:

    if ($_POST) {
            // create our data pieces
    	$user = new User($_POST ['name']);
    	$commentContent = $_POST ['commentcontent'];
            $comment = new Comment($user, $commentContent);
    	
    	
    	//not quite sue what this means
            /**
            * To answer this question, this is writing the data to a file called comments.html
            */
    	$handle = fopen("comments.html","a");
    	fwrite($handle, "<b>" . $comment->author->getName() . "</b>:<br/>" . $comment->getContent() . ",<br/>");
    	fclose($handle);
    	
    
    
    }
    
    

    BTW, this is just an example. Hope it helps you get started.

  5. instead of

    $name = $GET_POST['fullname'];
    $email = $GET_POST['email'];
    $message = $GET_POST['message'];
    

    try

    $name = $_POST['fullname'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    

    And it would be a good idea to read up on GET and POST in the manual.

  6. php uses .(dot) concatenation, not +

     

    echo "site_images/" . $id . "/" . $image;

     

    But I would do it in a more readable fashion; like this:

    $src = (empty($image)) ? "upload/your-photo.jpg" : "site_images/$id/$image";
    <img src="<?php echo $src ?>" />
    

     

  7. I have to preface this with 'I'm not a PDO expert so if I miss-speak, please don't hold it against me.'

     

    The quick and simple explanation is that PDO gives you one universal interface for accessing your db. You call $pdo->query() and it knows that you have a MySQL server and so to use MySQL calls internally. That's an abstraction layer; you abstract the actual calls out of the client code and into an abstraction layer.

     

    Imagine you set everything up on a MySQL server. You then find out MySQL goes bankrupt and you need to replace it with a competitor. How long do you think it would take to go through all your code and replace all the calls to MySQL and also alter functionality for handling the results of those calls. With PDO or a similar layer, you would change the details in your PDO config and all your client code calls would remain the same.

     

    PDO

     

    Abstraction layer

     

    Don't leave questions unasked for fear of stupidity; It's stupid not to ask. If he's a real friend, he won't treat you like an idiot :)

  8. Updates are fine if you're updating. Insert's are for new rows.

     

    I think you're main error is that you never fire the query, but I've edited your code for several different things.

    1. Added curly braces on line 4 - not necessarily needed in this case, but I consider it good practice so I threw them in there.

    2. moved the else condition braces around the rest of the code to only run when there's a session; this was an assumption.

    3. not sure what db engine your using in connect.php so I marked where you need to fire the query.

    4. added indentation

     

    I didn't test it but it should be good.

    <?php
    include 'connect.php';
    include 'main.php';
    if(!isset($_SESSION['id'])) 
    { // added curly braces
    	echo "You need to login to view this page";
    }
    else
    {    
    	// added indents
    	$id = $_SESSION['id'];
    	$firstname = $_POST['firstname'];
    	$lastname = $_POST['lastname'];
    	$motto = $_POST['motto'];
    	$bio = $_POST['bio'];
    
    
    	if(empty($firstname) || empty($lastname) || empty($motto) || empty($bio)){
    		echo "You didn't fill out any fields.";
    	} else if (strlen($motto) < 5) {
    		echo "Your motto must be more than 5 characters.";
    	}
    
        	$sql="UPDATE users SET firstname='$firstname', lastname='$lastname', bio='$bio', motto='$motto' WHERE id='$id'"; 
        	// you never actually fire this query...do so here
    }
    // moved this to wrap the code
    ?>
    
  9. I was thinking the same, but it seems like over kill for 3 videos?

     

    I would normally agree, but there's great potential with the historical data this would generate. You could see what you showed on Jan 24th for example.

     

    OP: I'd do it with a cron and a simple query on the page.

    CREATE TABLE `videos` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(50) NOT NULL,
      `url` varchar(255) NOT NULL, // assuming these are actually stored in a file structure
      PRIMARY KEY (`id`)
    )
    
    CREATE TABLE `video_views` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `vid` varchar(50) NOT NULL, // FK from video table
      `date` timestamp NOT NULL,
      PRIMARY KEY (`id`)
    )
    
    // CRON
    // query for 3 videos from videos table - this is where you'd add your additional business rules for which videos to display ie: per week, per month, etc.
    // insert them into video_views table with a timestamp/date
    
    // PAGE
    // query for all videos with today's date
    // show them
    
  10. By what you're saying, that all you did was change the name of a variable, there's no way it wouldn't work by changing it back. I thought about a reserved word clash but you said changing it back doesn't make it work again. It can therefore NOT be the name and you HAD to have done something else. That's logic. I'm sorry you disagree.

     

    Maybe if you post more code, it would help... :shrug:

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