Jump to content
Larry Ullman's Book Forums

margaux

Members
  • Posts

    453
  • Joined

  • Last visited

  • Days Won

    52

Posts posted by margaux

  1. I think you actually want to use a checkbox for your agree to T&C and I agree with HartleySan so I've removed the empty a element as it isn't doing anything.

     

    Change your form to

    
    <p><?php create_form_input('Agree', 'checkbox', $reg_errors); ?><label for="Agree"> I agree to the user Terms and Conditions.</label></p>
    

    and you will need to ensure that your create_form_input function includes some code for input type checkbox. I don't have this book but something like

    if (($type == 'text') || ($type == 'password') || ($type == 'checkbox')) {
    

    Your validation will then be

    if (isset($_POST['Agree']) && ($_POST['Agree'] == TRUE)) {
    $Agree = mysqli_real_escape_string($connect, $_POST['Agree']);
    } else {
     $reg_errors['Agree'] = 'Please agree to the user Terms and Conditions.';
     }
    

     

    You have some non-standard practices in your code such as including styling within the html, using a capital letter in your your variables e.g. 'Agree' and not using ascii codes for special chars in your html e.g. change '-' to . I hope you don't mind my pointing these out but its good idea to use good practices. On some forums (not this one) people won't help unless you use standard practices.

  2. As well as creating a site about a topic you're interested in, you could create your own site which would include your portfolio (just make it up initially), a blog which you hand code rather than using something like wordpress or joomla, a contact form, social media links and an xmlfeed. Other ideas are to do some sites for local charities or community projects.

     

    Regards software - I use a basic text editor, TextWrangler. It provides colour coding for different languages which is very useful. I've heard very good things about TextMate and keep meaning to have a look at it.

  3. I would just always select the X latest rows and let it be with that. DBMS systems can handle huge sets of data, so I would not worry about that. If it should become a problem, run a clean up function once in a while instead.

     

    That seems like a better approach but how does that cater for items that are no longer on the feed but will still be on the database and returned by search queries?

  4. HEREDOC is cool but for me it is a little ugly, may come to use it though.
    I also agree that its ugly but once I used it for some lengthy php/ html mix and I was converted. Is it bad form or just not pretty?
    for simpler stuff like the price, name and description it seems easier to just concatenate.
    I do too. For different situations its good to have a couple of options you can fall back on to make things simpler.

     

    MVC - stands for Model, View, Controller. Its a programming approach to keep your code organised and easier to maintain especially good when working on large sites involving many people. Many frameworks follow this approach. The model is the data, the view is the presentation and the controller is the logic that binds the model and the view. As an analogy think of good website development - you separate the html from the css and the javascript so you are not mixing presentation with styling with behaviours.

    • Upvote 2
  5. There are a number of ways you can code this. Personally, if I'm coding alot of html mixed with php processing and variables I don't use echo. When you need to some php processing, just insert it within <?php and ?> tags. Be sure to not use the shortcode tags as they have been deprecated.

    <table class="myclass"><tr><td><input name="display_y" type="radio" value="Yes" checked=" <?php if($snippets_list['display']='y'){echo 'checked';} ?>"/></td></tr></table>

     

    An alternative is to use echo with the heredoc syntax.

    • Upvote 2
  6. I'm working on a site which reads in data from an xml feed and creates a d/b table. The feed is continually being updated so the table also has to be updated frequently. I think the feed will be accessed several times/hour to refresh the database. Is the better approach to

     

    1. Drop the table and recreate from fresh everytime the feed comes in. This ensures that the database doesn't continually grow with items that have already been stored. However how will site visitors be affected if the table is being created while they're viewing?

     

    2. Create the table once and update it subsequently. Check for each item's unique reference - if it exists then overwrite it, that way you pick up any changes to the item. This approach seems less attractive from a speed point of view. Also it means that items that are no longer on the feed will still be on the table and there will have to be some kind of manual reconciliation?

     

    Or is there another option?

     

    Thanks for your thoughts.

  7. Actually, I'm thinking I was incorrect. I created a blog for someone and originally had a similar query (note my order by is DESC).

    $r = mysqli_query($dbc, "SELECT title, excerpt, content, image_file, DATE_FORMAT(date_created,'%D %M %Y') AS pubdate FROM articles ORDER BY pubdate DESC LIMIT 5");
    

    The problem was that the rows were being ordered by day rather than the whole date, which technically makes sense. But I was getting the rows in this order

    7th March

    7th June

    6th May

    5th January

    3rd April

    Then I changed the query to

    $r = mysqli_query($dbc, "SELECT title, excerpt, content, image_file, DATE_FORMAT(date_created,'%D %M %Y') AS pubdate FROM articles ORDER BY date_created DESC LIMIT 5");
    

    and the rows returned as follows

    7th June

    6th May

    3rd April

    7th March

    9th February

     

    This thread is a good illustration of where aliases come into their own. In the above query, without an alias I would have had a cumbersome bit of code to access the date field e.g.

    $r = mysqli_query($dbc, "SELECT title, excerpt, content, image_file, DATE_FORMAT(date_created,'%D %M %Y') AS pubdate FROM articles ORDER BY date_created DESC LIMIT 5");
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
    echo "<h2>$row[title]</h2><p> $row[excerpt]</p><p> $row[DATE_FORMAT(date_created, '%D %M %Y')]</p>";
    }
    

    Much easier to code

    $row[pubdate]

    and much less room for error!

     

    Anyway Leo - I hope this helped a little and didn't cause you any confusion.

     

    And thanks Antonio - you are correct that the column must be selected to use it for ordering.

    • Upvote 1
  8. I would have thought the error in the query was due to the ORDER BY clause. In your query you create an alias for the date_registration by do not use it in the ORDER BY clause. It should be

    $q = "SELECT last_name, first_name,
    ➝ DATE_FORMAT(registration_date, '%M
    ➝ %d, %Y') AS dr, user_id FROM users
    ➝ ORDER BY dr ASC
    ➝ LIMIT $start, $display";
    

     

    Its definitely worth spending time on the debugging chapter - you will save yourself loads of time in the future and learn a few tricks. One quick trick is to insert something like

    print_r($r) 

    in your code which will give you full information about your query in a more readable format.

    • Upvote 3
  9. I'm trying to display an rss feed on a webpage and could use some help getting the data. The format of the feed is

    SimpleXMLElement Object
    (
    [title] => HBR.org
    [id] => tag:blogs.harvardbusiness.org,2007-03-31:0.global-incremental
    [link] => SimpleXMLElement Object
     (
    	 [@attributes] => Array
    		 (
    			 [rel] => alternate
    			 [type] => text/html
    			 [href] => http://blogs.hbr.org/
    		 )
     )
    [updated] => 2012-10-22T11:10:17Z
    [subtitle] => Practical insights, tools and resources from leading business thought leaders.
    [logo] => http://cbimages.ed4.net/hbsp/9380_225659.gif
    [entry] => Array
     (
    	 [0] => SimpleXMLElement Object
    		 (
    			 [title] => The Sad Truth Behind Growing Clashes at the WTO
    			 [id] => tag:blogs.harvardbusiness.org,2007-03-31:126.12422
    			 [link] => SimpleXMLElement Object
    				 (
    					 [@attributes] => Array
    						 (
    							 [rel] => alternate
    							 [type] => text/html
    							 [href] => http://feeds.harvardbusiness.org/~r/harvardbusiness/~3/olpM1q4GSdM/the_sad_truth_behind_the_growi.html
    						 )
    				 )
    			 [published] => 2012-10-22T11:00:41Z
    			 [updated] => 2012-10-21T22:47:25Z
    			 [summary] => Don't be fooled by the rhetoric about protectionism.
    

    I want to get the title, href, published and summary data for the 3 most recent [entry]s. Code below is working for the most part but I dont know how to get to the href which is an attribute of link. I've tried a number of different syntaxes and foreach loops but unsuccessfully.

    //get the feed and create a variable to enable parsing of the data which is returned in xml format
    $feed = simplexml_load_file("http://feeds.harvardbusiness.org/harvardbusiness/");
    
    echo '<ul id="rssFeed">';
    $i=0;
    
    //parse the feed to get the title, link, published date and summary for each entry
    foreach ($feed->entry as $entry) {
    $pubdate = date("jS M, Y H:i", strtotime($entry->published));
    echo '<li a href="' . $href . '">' . $entry->title . '</a>
    <p>' . $pubdate . '</p>
    <p>' . $entry->summary . '</p>
    </li>';
    
    if ($i++ == 2) break;
    
    } //end foreach loop
    
    echo '</ul>'// end the ul tag
    

    Please can you suggest how I access the href value?

     

    Also I've used a counter and a break clause to stop the loop after 3 loops but I don't think this is the best practice. How would I get just the first 3 entries from the feed?

  10. An error occurred in script 'D:\xampp\htdocs\includes\form_functions.inc.php' on line 64:

    date() [function.date]: It is not safe to rely on the system's timezone settings. Please use the date.timezone setting, the TZ environment variable or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'UTC' for '5.5/no DST' instead

    The error message is telling you what the problem is and how to resolve it. The timezone is probably not set on your server so you need to set it using
    date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier.
    • Upvote 1
  11. I'm not surprised.

     

    The form is validated using js and php. The php script inserts the data to the d/b and sends an email. Only then do I want to display a message on the original page and have that message fade out on the click of a button (used some jquery for this). This message is inform the visitor his form data was sent (validation errors are handled earlier).

     

    My approach has been to code the message in a div with display:none.

    <div id="msg" name="msg">
    <h2>Message</h2>
    <p>blah blah blah</p>
    <p><input type="button" value="close" id="closemsg" class="formbutton"/> </p>
    </div>
    

    and the css is

    #msg
    {
    background:#fff;
    color:#b26fa7;
    border:2px solid #b26fa7;
    z-index:1;
    display:none;
    position:absolute;
    width:30%;
    height:100px;
    margin:50px auto 0;
    -webkit-border-radius: 8px;
    -moz-border-radius: 8px;
    border-radius: 8px;
    text-align:center;
    }
    

     

    When the php script finishes it redirects to the original home page with a $_GET variable set. The js above checks for this variable and sets msg div's position to block so it can be seen. Surprisingly everything is working except one detail - because I've set msg div's position to block, its slightly throwing out the page layout.

     

    Maybe I could use Ajax instead but I'm not proficient enough to use it. Maybe that's the answer to my original question 1. As for my other question, how would I use something like slice instead of charAt to get the characters after the equals sign from the url. As I'm using $_GET, the url is along the lines of www.example.com?action=display&id=2. I tried

    var action = window.location.search.toString().slice('=');
    

    I'll try again tomorrow but I was just happy the page worked.

  12. Some more searching and I discovered window.location.search and below is my function which seems to work. I'm going to try to tidy it up with split. Any other improvements do chime in.

    window.onload = function showMsg () {
    'use strict';
    var action = window.location.search.toString().charAt(3);
    if (action == 'd'){
    var div = document.getElementById('msg');
    div.style.display = 'block';
    }
    }
    

  13. I'm building a one page website which uses php, mysql and jquery to provide various functionality including form processing. After the form is processed I would like a simple message to be displayed which can then be hidden by clicking a button.

     

    What would be the best way to achieve this. I was thinking of displaying the message in a div which could toggle between display:none and display:block. The php file which processes the form and inserts the data to the d/b could redirect to the same page with a hidden field which could be checked and if set could use javascript to set the message div to display:block.

     

    My questions are:

     

    1. is this a good way to achieve this or would you suggest an alternative approach?

    2. if I used the above approach, how do I get the javascript to check if the hidden field is set in the $_GET variable? I did a little seach and saw something about using decodeURIcomponent. Before I start looking into thIs more, is decodeURIcomponent a good method, are there others? and any tips on using decodeURIcomponent?

     

    Thanks for any suggestions.

  14. Try something like this

    // Display all the Paintings.
    $result = mysql_query ($query);
    while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
    // Display each record.
    echo "<div class=\"prints\"><a href=\"view_print.php?pid={$row['print_id']}\">
    <img src=\"Uploads/{$row['image_name']}\"height=\"130\" $image[0] alt=\"{$row['print_name']}\" /></a></div>
    \n";
    } // End of while loop.
    mysql_close(); // Close the database connection.
    

    The above code outputs your html, then you need to style it to make it look nice. You should avoid mixing html and css in code as you originally did for a number of reasons which you can read about. Presumably you have an external stylesheet? Add the following to that file, though you may need to adjust the margins and padding.

    .prints {
    width:33%;
    float:left;
    padding:10px;
    }
    

     

    If you dont have an external filesheet, you should. But if this is just practice you can add the styling to the head section of your page - just surround the css code with <style></style> tags.

     

    rob is right - this thread is for php and you're asking questions about basic html and css. Ask away, just do it in the right place.There are loads of resources on and offline that will teach you what you need to know. One book that looks pretty good is HTML5 and CSS3, published by the same publishers that Larry uses. Its good as it will intro you to HTML and CSS and then show you some HTML5 and CSS3 additions. Spend some time learning the basics and you'll save yourself alot of time in the long run. Oh, and NEVER use all caps for your html tags.

×
×
  • Create New...