Categories: MySQL, General

05/10/08

Alternate Colors on MySQL result rows

Permalink 09:56:38 pm, Categories: PHP, MySQL  

How to Have Alternating Colors in Table Row Results

1. Pick two colors for table cell backgrounds. For example, light cream and light green.

2. When you dip into the MySQL table, you will be using a while loop. You will also be using two other elements.

3. The first is the use of $i=0, $i++ while using the “while loop”

4. The second is the use of the modulo % to get the remainder.

5. As you use the “while” loop, you increment the $i by 1 each time, and you will be dividing $i by 2, $i % 2.

6. You also place the condition, if the remainder is 0, then use the light cream, and if the remainder is 1, then use the light green.

Note, if you want three colors, just do the same thing, use $i % 3, and if the remainder is 0, 1 or 2, then use color1, color2 or color3. Obviously, if your site is gay, and you want the gay colors to appear, use $i % 6, assigning 6 colors, but I think that would look tacky. I think red and white with a little blue and some stars appearing in the background would look like an American flag.

Val Zubiri

Programming by Val Zubiri
Art by Val Zubiri
Arts Global Impact.org / Original Art and Art Books for Collectors for sale
Chicago Art Movement.com
Get a Massage Contact and Shop using your mobile phone

04/25/08

How to Make Forms with Error Messages

Permalink 10:33:57 am, Categories: PHP, MySQL  

How to: Making Forms with Error Messages in PHP and MySQL

What you need to understand to make forms with error messages.
I use Dreamweaver for this.

1. Your form has an action="". In between the two quotation marks, place your PHP_SELF code. Simply Google the term “PHP_SELF” for detailed explanation. Simply put, the PHP_SELF code, placed in the action part of the form, will make the submit button come back to that same form page.

2. Your page needs PHP coding before the entire HTML code on that page. What this does is that PHP gets “computed” before the HTML gets sent by the server to the visitor’s computer. So, when the visitor (or user) fills up the form, and presses submit, the server “computes” the answers in the form before serving the same page again.

If the answer is unacceptable, like some fields in the form were left blank, the page will display a message telling the visitor what mistake he or she made.

3. Your form also needs what is called a “hidden” variable. The variable is not seen up front on the form page, but if you look at the html code, the hidden variable should be there. Usually coders place the hidden variable next to the submit button code.

It looks like this:

Code:

<input type="hidden" name="stage" value="2">

Make it a practice to place it near the submit button, like so:

Code:

<input type="submit" name="Submit" value="Submit">
<input type="hidden" name="stage" value="2">

I usually use the name “stage” and the value “2″, because I imagine that the value “1″ would be the first time the user or visitor sees the form, and the second time or stage the visitor sees the form is right after the visitor pressed submit.

4. Somewhere on the page, but usually just right on top of the form, would be where error messages, preferably in red, should be. However, that would also be the spot where the Success! message can be read.

Examples of error messages are:
You need a name. The name should not be left blank
You need a last name. The last name should not be left blank
You need an email.
Your email is not in the correct format. It should be in the form “youremail@thegivendomain.com".
You need a username
The username is already taken
You need a password
The password is too short
The password is too long
You forgot to make a comment.

If all the required fields are answered correctly, the information gets processed. An example of a success message would be:
Success! You have been registered. Please expect an email notifying you of your registration. Welcome Harold4000!

5. You need to learn to check the answers in different ways. The answers are strings.

“You need a name” means that the field was left blank… so your PHP code would say something like
if $name =="", then the message appears. “” means that there is nothing submitted, and therefore, the variable $name is “” instead of “Harold". There’s nothing in between the two double quotes.

The Email used… The code checks if the email is valid, by checking if the “@” sign is present, and the “.” period is present as well. Before the “@” sign, there should be at least one character, and there should also be at least one letter in between the “@” and the “.” period. There should also be some letters after the period.

Note: If you are also being a little more industrious, there is a way to check if the domain after the “@” sign is a valid website. However, depending on the code you use, and your server, it might not work at times, resulting in you irritating your potential user or client. So if you are a vengeful, compulsive, “gotcha!” type of person, research on this functionality, and implement it. Otherwise, just let some gibberish from some users be… like “kfjdkfjw@dlfdkfjd.cokdjfk". Not everyone will do this hundreds of times anyway.

You can require your password to be an acceptable 4 to 8 characters, so if the user puts in 3 or 9 characters, you would tell them it’s unacceptable.

6. Don’t go yet, it gets even more complicated. There are two important parts to code.

7. The first part of the code. Assign variables to all the error messages. So the first error, for example, is $errormessage1, and then $errormessage2, $errormessage3, and so on, until you get to the success message, which you can name $success1 or $errormessage9

8. The second part would have the conditions. It will be a series of if - else - if - else - if -else.

So “if” the first condition is wrong, then $errormessage = $errormessage1. “Else” (if the first condition is okay) move on to see if the second condition is met. If not, then $errormessage = $errormessage 2. Else, (if the second condition is okay) then move to see if the third condition is okay. And this goes on, until all the conditions are met.

If all the conditions are met, then further action is taken. For example, the information would then be made to go to the database OR an email can also be sent to the user, and $errormessage = $success.

9. Notice that all the error messages are to be programmed as:
$errormessage = $errormessage1, etc… Each error message takes a turn to become $errormessage, without the number.

10. Go to your html code and view the design, not the code. Type in what I would call a “marker” - something like “xxxxxxxx", then make it bold, and color it red. Then go look at your html code and look for “xxxxxxxx". Replace it with PHP code:

Code:

<?php print ("$errormessage");?>

So there you go, that’s the way to have a self-correcting form.

Sometimes, you may want to code, together with the error messages, the new line or paragraph tags, and the bold and the color tags. That way, the page only expands down when there is an errormessage to be shown.

There are many ways to make this even more complicated.

Your email thanking the user for the successful submission can be formatted to look like an html page. The email itself uses the PHP email function. Google using the words “headers” “email function” and “PHP” for more information about this.

You may need for the user to click an activation link in the email with a randomly generated code in order to fully register him or her. This means you need to generate unique random codes.

Usernames should also be unique, which means you need a stage when you should check if the username is already in use.

You might also want the form fields to automatically populate themselves. For example, if you have First Name and Last Name, and the user forgot the Last Name, when the page returns, the First Name should already have the field answered so that the user does not have to refill the field.

In the past, a lot of forms had the Submit Button and the Reset Button. When you press reset, the form becomes blank again.

However, a lot of websites now do not have the Reset Button. The rationale for this is that users should only use the form when they seriously do want to fill up the form and submit. Resetting forms back to no answers at all only means that the user is merely playing with the form to begin with.

There is also another part of the PHP code you should not forget to consider.

In the past versions of PHP, variables (the answers in the forms) get sent to where they should go without you having to worry if they were even passed on in the first place.

Now, the later versions of PHP need you to “register” the variables so they can be “remembered” to come back on the page.

The code would look like this, repeated for every variable on the page, and placed on top of the PHP code at the top part of the page.

Code:

$theVariable = $_POST["theVariable"];
$theName = $_POST["theName"];
$theLastName = $_POST["theLastName"];
$stage = $_POST["stage"];

Val Zubiri;D:>>

Programming by Val Zubiri
Art by Val Zubiri
Arts Global Impact.org / Original Art and Art Books for Collectors for sale
Chicago Art Movement.com
Get a Massage Contact and Shop using your mobile phone

03/03/08

My Secret, Fast Way to Edit Arrays for PHP and MySQL

Permalink 10:15:46 am, Categories: PHP, MySQL, General, General  

My FAST way to enter massive amounts of array data on MySQL tables using PHP, InDesign, Excel, Frontpage, Dreamweaver and Notepad

I’m sure this is for the novice, because people who work with PHP, MySQL and others eventually find ways to enter data formatted as arrays in a really fast manner. This is my way that does the job.

Let’s say you need to make a MySQL table that has a lot of information, like from old database files, or the entire County, City, State, Zip code, Longitude, Lattitude of the United States.

For PHP and MySQL, the data needs to be in this form


"insert into nameoftable (first_name, job, city) values ('Stan','doctor','Chicago'), ('Charlie','engineer','New York'), ('Alfonso','consultant','Miami')";

It looks confusing, but using tabs, the statements looks better…


"insert into nameoftable (first_name, job, city) values
('Stan','doctor','Chicago'),
('Charlie','engineer','New York'),
('Alfonso','consultant','Miami')
";

If you have thousands of imported data, or even hundreds, this could take weeks to format and check.

I use old versions of Microsoft Excel, Adobe InDesign and Notepad or Frontpage.

First, select all of your data and paste it to Excel. See if the carriage returns and the tabs are there. The data should be in many rows and many columns. If they are just in one long row, then try pasting the data in InDesign.

In InDesign, see if the data appears as if they have been separated by tabs, or commas, and hopefully, in separate lines.

Before you can assign the single quotation marks ('), the commas (,) and the parentheses - ( ) , you need to clean your data first.

How I do this really fast

1. In InDesign, make each line the row of entries for the MySQL table. Use the Enter button to assign new lines to your information. Don’t worry if you miss an entry or make a mistake. Just do this as fast as you need to so.

2. Tab to separate the data, if the tabs are not there. If your information is separated by commas, then don’t do this step. Don’t worry about the spaces in between the data. You will clean it up later

3. Clean up the possible spaces between tabs and new lines using Search and Replace. The good thing about InDesign is that you can search for tabs using “^t". So to search for the extra space before a tab,
type in ” ^t” in the search box, and
type in “^t” in the replace box.
Click Replace All. Do this several times, until all of them are gone. You may have had something like
” ^t” - several spaces before the tab,
so keep clicking until a message that says nothing more was found appears. Then do the same thing with spaces after the tabs by
searching for “^t ” and
replacing it with “^t".

4. Do the same thing with the paragraphs. Clean up the possible spaces before and after paragraphs by
searching for ” ^p” and
replacing it with “^p".
Then do it again with “^p ” and
replacing it with “^p”

5. Make sure you save your work once in a while. Your computer could crash or freeze anytime with all this search and replace procedures.

6. Everything still looks jumbled up, so what you now need to do, is to see if they are tabbed correctly. Copy all, or a manageable amount and paste them on an Excel table. If you missed a tab or carriage return, you will spot it here. Make the necessary corrections.

7. Use Excel to rearrange your columns or take information off, or insert new columns with additional information, like if you want to put a “paid, unpaid” column, etc. Or if you want to just sort the rows according to last name, or city or whatever, of if you want to sort and separate the data to be put in different tables.

8. Copy and paste the data back to Indesign. Now you need to replace the tabs and carriage returns, but you’re still not safe. Save the data.

9. Look for single quotes first. If they are there, they will confuse PHP. You need to replace “‘” with “\’". However, don’t fully trust this yet… Look for a symbol that is not in the data. For example, look for an exclamation point “!". If your data has none, then search and replace your single quotes with five or more exclamation points - “!!!!!!!!". Save the data.

10. Search and replace tabs. Search for “^t” and replace them with something like “#######, %%%%%%%” - several sharp symbols - a comma - a space - several percentage symbols. Save the data.

11. Search and replace the new paragraph lines or returns. Search for “^p” and replace them with “&&&&&&&&&amp;),^p(++++++++” - several ampersands - a closing parenthesis - a comma - ^p - an opening parenthesis and several pluses. Save the data.

12. Make a new file on Frontpage, Dreamweaver or Notepad. In Frontpage, click the HTML tab, and erase all the HTML code. In Dreamweaver, click the Code button, and erase all the HTML code. Copy and paste the data. The drawback about Notepad is that it can be limited in what it can take, and cut off your data, so make sure that the last line on Notepad is the last line you have on the InDesign file.

13. Replace the “!!!!!!!!” in (8) above, with “\’". Replace the “#######” in (9) above with “‘", and do the same thing with “%%%%%%%", “&&&&&&&&", and “++++++++", replacing them with “‘".

14. The reason we are not doing (12) above in Indesign is that InDesign will give you opening and closing single quotes, not the vertical, simple single quotes (I don’t know what you call them).

15. Look at the last line, and take out the comma if there is one. Now you have formatted data that you can enter into your MySQL table.

16. This is a really fast tutorial. Sorry for grammar mistakes or inexact commands for the programs I mentioned, but you get the gist. You may need to look at (12). Depending on how you set up your MySQL, you might want to put in a different unicode for the “‘".

Val Zubiri:P

Programming by Val Zubiri
Art by Val Zubiri
Arts Global Impact.org / Original Art and Art Books for Collectors for sale
Chicago Art Movement.com
Get a Massage Contact and Shop using your mobile phone

02/26/08

Online Tutoring

Permalink 11:07:52 pm, Categories: PHP, MySQL, General, General  

When I finally began to understand PHP and MySQL, I realized that a lot of people who would also like to learn PHP and MySQL do not need to go through the same, or worse, difficult ordeal that I went through.

There were times when I had to go to Borders Bookstore, hang out in the Computers section, and wait for a person to pick up a PHP or MySQL book. I would then approach them and ask them if they can sold my PHP/MySQL problem.

Online, I have also looked for member profiles which have the keywords PHP and MySQL mentioned. I would email them my questions, and I got answers back most of the time.

By doing this, and also mentioning in my own profile that I liked PHP and MySQL, I also encountered people online who needed help.

The best way I was able to teach anyone PHP and MySQL, later on, was to ask them to first buy the book that I also already have. If my chat buddies got stumped, I would refer them to the book page, and proceed from there.

;)Val Zubiri:D

Programming by Val Zubiri
Art by Val Zubiri
Arts Global Impact.org / Original Art and Art Books for Collectors for sale
Chicago Art Movement.com
Get a Massage Contact and Shop using your mobile phone

02/24/08

Client Wants Members To Be Able To Login

Permalink 10:24:31 pm, Categories: PHP, MySQL, General, General  

CASE: The Client wants the website to have a login page.

What this means is that your life just got more complicated.

You will need the following pages:

  1. Registration Page
  2. Login Page
  3. Logout Page (or Button)
  4. Page that automatically emails a forgotten username or password

and: A database of username, password and email

The Registration Page
Should check the database so that the username and the email are not repeated (be unique).

The Login Page
Should either use sessions or cookies which the Logout Page clears

The Forgot Username / Password Page
Should have the PHP mail() function. You might also consider making the forgot password email look better than just having regular text. (You will need to know about html headers in the mail function.)

B)

Actually, this analysis continues…

The client surprises you with such a request, but he or she has valid reasons for such a request. Chances are you would have to make more tables and more columns in the database.

Then you get more creative…

Elements the Registrant would not know your are recording
It would also not hurt to have a column that would tell the date the person registered. It might not hurt to know when the member last logged in. It might not hurt to know how many times the website was used by the member. It might also not hurt to know the IP address of the registrant.

Elements the Registrant would know your are recording
During the registration process, you might want to know age, city, interests, sex, etc. Whatever you do, don’t scare off the registrant by asking too many questions.

Val Zubiri;D

Programming by Val Zubiri
Art by Val Zubiri
Arts Global Impact.org / Original Art and Art Books for Collectors for sale
Chicago Art Movement.com
Get a Massage Contact and Shop using your mobile phone

:: Next Page >>

September 2010
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

PHP and MySQL

Tips on the use of PHP and MySQL for your website. If you need help with your site, ask and I'll see if I can help. If you need long-term help with your website, let me know as well.

Search

XML Feeds

What is this?

powered by b2evolution free blog software