Jump to content

adam_bray

Members
  • Posts

    101
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by adam_bray

  1. A form isn't your typical layout element and isn't really used for styling like you are.

     

    From what you've said.. your problem is that the form is display: block; change it to display: inline-block; and it should be sorted, but personally I'd wrap it in a div and style that.

    • Like 1
  2. You're using $active wrong -

    $active = $query->fetch_array(); // $active is now an array
    if($active == 1) // $active is an array, it will never == 1
    $active_update = "UPDATE $tbl_name SET $active=0"; // use $active as a string
    

    What are you trying to update at the end?

     

    For your current error you need to change -

    echo $query;
    // change to (in the same place)
    print_r($active);
    
    

    You're also going to get warnings about the $_GETs because they may not be defined -

    $user = (isset($_GET['user']))? $_GET['user'] : 'invalid';
    $user_id = (isset($_GET['id']))? $_GET['id'] : 0;
    
    if($user == 'invalid'] || $user_id == 0) {
    // error code
    }
    
  3. $id = "1";
    $query = "SELECT * FROM table WHERE id = '$id';
    $result = mysqli_query($query);
    
    while($data = mysql_fetch_assoc($result))
    {
     echo $data['title'];
     echo $data['content'];
     echo $data['author'];
    }
    
    You need to be careful following this code as it's not very secure. The code Strider posted is better as it uses prepared statements.

     

    Assuming you go down the same route most beginners go with, you'll next want to show individual posts on each page, meaning you'll end up with this -

     

    $id = $_GET['id']; // Here's the problem
    $query = "SELECT * FROM table WHERE id = '$id';
    $result = mysqli_query($query);
    
    while($data = mysql_fetch_assoc($result))
    {
     echo $data['title'];
     echo $data['content'];
     echo $data['author'];
    }
    
    This will leave your code open to SQL injections which isn't a good idea. The idea being that if the user passed the following string to your code where you're searching for the ID, you could lose a lot of data.

    ' or 1=1 UNION DROP TABLE table;
    
    Strider's code uses prepared statements which is a much safer way of passing variables to your query.

    This:

    $sql="INSERT INTO article (title, content, author)
    VALUES ('$title', '$content', '$author')";
    
    if (!mysqli_query($con,$sql)) {
      die('Error: ' . mysqli_error($con));
    }
    
    Becomes this:

    /* Set the SQL Statement */
      $sql = "INSERT INTO article (title, content, author) VALUES ( ?, ?, ?)";
      /* Prepare an SQL statement for execution */
      $stmt = mysqli_prepare($con, $sql);
      /* Binds variables to a prepared statement as parameters */
      mysqli_stmt_bind_param($stmt, "sss", $title, $content, $author);
    
    Have a look at the following, try typing in the functions you don't understand into PHP.net and you'll find good examples of what's happening.

    <?php
    
    // Connect to MySQL DB
    $mysqli = new mysqli("localhost", "my_user", "my_password", "world");
    
    // Check connection
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    
    // Look for the post ID
    $id = $_GET['id'];
    
    // Create a prepared statement
    if($stmt = $mysqli->prepare("SELECT title, content, author, date FROM table WHERE id=?")) {
    	
        // Bind parameters for markers
        $stmt->bind_param("s", $id);
    // Run query
        $stmt->execute();
    	
    	// Loop through the results
    	while ($obj = $stmt->fetch_object()) {
           
    	    $results = 
    		'<h1>'.$obj->title.'</h1>
    		<p>'.$obj->content.'</p>
    		<p><em>Posted by '.$obj->author.'</em> on '.date("d F Y",$obj->date).'</p>';
        }
    
        // Close statement
        $stmt->close();
    }
    
    // Close connection
    $mysqli->close();
    
    ?>
    <!doctype html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Untitled Document</title>
    </head>
    
    <body>
    <?php
    	if( count($results) > 0 ) {
    		echo $results;
    	}
    	else {
    		echo '<p>There are no posts to display</p>';	
    	}
    ?>
    </body>
    </html>
    
  4. You should also tidy up your HTML, using line breaks like you have is pointless.

    '<h1> Welcome To The Site! </h1>
     <p>This site will let you post your ideas!</p>';
    

    Use CSS to specify margins and padding between elements, not line breaks.

  5. I see you've added a single wrapper to your page, the way I'd do it would be to add 2 or 3.

    <div id="topbar-navigation" class="clearfix">
    	<div class="wrapper">
    		<div id="logo"> <!--FLOAT LEFT-->
    			<!--LOGO HERE-->
    		</div>
    		<nav> <!--FLOAT RIGHT-->
    			<ul> <!--NO FLOAT-->
    				<li>...</li>
    			</ul>
    		</nav>
    	</div>
    </div>
    <div id="main-container" class="wrapper clearfix">
    	<!--CONTENT HERE-->
    </div>
    
    
  6. Looking through your CSS, I'd change a couple of things.

    1. You've set a height on your main container, I presume that's because the background disappears without it. If so, look at using a clearfix
    2. You should create a wrapper class that sets a standard width across everything (header, content, footer) -
    div.wrapper {
    margin: 0 auto;
    width: 1000px;
    }
    
    1. continued... with the wrapper in place, set the <ul> to float: right;
  7. Absolute positioning most probably isn't the answer as it doesn't allow much flexibility at all. Do you have a link to where you're doing this?

     

    You don't need to specify margins and padding over 4 rules, you can combine them into 1 rule. There also shouldn't be a space around the -

     

    This

    margin - left: auto;
    margin - right: auto;
    margin - top: 20px;
    

    Should be

    margin-left: auto;
    margin-right: auto;
    margin-top: 20px;
    

    But this works better (read up on the web developers compass)

    margin: 20px auto 0;
    
  8. Wouldn't the best option be to combine both of Psycho's suggestions?

     

    Store a list of servers in a database, along with the time they were last pinged and the status it returned (option 1).

     

    Load the page without the server statuses, then use AJAX to run a SQL query, you can then check the ping time of each result and ping it again if needed. That way the page will load pretty fast and do all the heavy lifting in the background.

  9. I'm looking for some clarification here from different viewpoints to understand real world applications.

    In a previous thread, I suggested to someone that they read up on singleton methods to restrict class duplication (oops!), I was quickly (and rightfully) shot down. I did this after having read through blog posts that also suggested singleton design to stop multiple MySQL connections. At the time I didn't consider that could be useful to some people.. fair enough.

    Thankfully I don't use singleton methods within my own code, but I do use static methods for most things. Reading through numerous blog posts, tutorials, etc.., it seems like static methods can also be considered anti-design and is something to avoid.

    So now it seems I'm at a point where I need to rewrite my existing framework & CMS, probably using dependency injection within my classes. I understand how this works, and why it makes sense.

    What I'm struggling with is understanding how to use dependency injection within a (personal) CMS application.

    For example - 

    • I have a config.ini file
    • I have a class that reads the .ini file, stores the variables, and provides me methods to access them
    • I have a content class that selects the relevant page/component from the DB (db & config dependency), then displays it via my template engine.
    • Within the included view files I call component classes (articles, contact, etc..), each of these require a connection to the DB, which has a config dependency.

    Here's some code to explain it better -
    index.php

    <?php
    $settings = '/config/config.ini';
    
    $config = new Config($settings);
    
    $db = new Database($config);
    
    $content = new Content( $db ); // Config may also be passed for content config - keeping it simple for example
    print $content->loadPage($_GET['page']); // This would now include the code below
    ?>
    

    Let's say that this then loads the article index (through $content->loadPage()). The view would look something like this -
    article_index.php

    <?php
    // Duplicated code
    $settings = '/config/config.ini';
    
    $config = new Config($settings);
    
    $db = new Database($config);
    
    // Article code
    $articles = new Articles_Model($db);
    return $articles->getArticles(0,15);
    ?>
    

    Now my problem is that I'm duplicating the config and db class calls for no reason.

    Is the sollution to store these within a registry class? But then I'm creating globals, which again seems anti-design.

    Or is the problem how I load the active page?

    Any insights would be much appreciated.

  10. Where are you defining $Fake? From what you've posted I don't see a query being executed.

     

    I don't think you'd want to insert all the email addresses into 1 $to variable, you'll probably want to loop the process, otherwise you'll find all emails will be flagged as spam.

     

    You also need to switch from mysql_* functions to mysqli_* .

  11. Your table structure doesn't look that great from what you've posted. I would use something along the lines of -

     

    Stream
    status_id (auto increment)
    status
    uid
    status_time
     
    Users
    uid (auto increment)
    username
    avatar
     
    Likes
    status_id
    uid
    like_time
     
    Comments
    comment_id (auto increment)
    status_id
    comment
    uid
    comment_time
     
    Then as you mentioned, you need to use JOIN's to get the relevant data.
     
    If you're not sure on the type of JOIN to use then think along the lines of "can this value = null, but the query should still work?"
    - yes = left outer join
    - no = inner join
     
    With that said, here's an example query for the table structure I suggested above (untested) -
    SELECT stream.status 
    , users.username AS author_username
    , users.avatar AS author_avatar
    , c.comment
    , c.comment_time
    , uc.username AS comment_username
    , uc.avatar AS comment_avatar
    , COALESCE(l.likes,0) AS likes
    FROM stream
    INNER
      JOIN users
      	ON users.uid = stream.uid
    LEFT OUTER
      JOIN comments AS c
      	ON c.status_id = stream.status_id
    LEFT OUTER
      JOIN users AS uc
      	ON uc.uid = c.uid
    LEFT OUTER
      JOIN ( SELECT like_time,   
    		COUNT(*) AS likes
    		   FROM likes 
    		 GROUP
    		   BY status_id ) AS l
    	ON l.status_id = stream.status_id
    ORDER BY status_time DESC
    LIMIT 50;

    To answer your second question I'd suggest setting up the MySQL, entering some dummy data, then running the query in phpmyadmin to see how it gets returned. Once you understand that, it'll become much easier to work out how to loop through the rows.

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