A clever little thing

There is a clever little thing I found in some tutorials I’d like to share^^

When I was considering adding the multiple search function for my gallery I found it’s not easy. I can’t directly write the “SELECT” sentence out because some variables might be empty. I should judge if they are empty first and then add them to the “SELECT” sentence. After writing a long string of “IF…”, I found I don’t know how to deal with one thing. I couldn’t add “AND” before the first variable but should add “AND” before others in the “SELECT” sentence. But which one is the first one keep changing. I search some tutorials online and found that they do it in a clever way, as following:

if($A1 != null){
$A2 = ” AND X=’$A1′”;}
if($B1 != null){
$B2 = ” AND Y=’$B1′”;}

$search = “SELECT * FROM table1, table2 WHERE 1=1 “;
$search .=$A2
$search .=$B2

At first I didn’t understand what 1=1 is, and suddenly I found it is just a sentence which is always true. You can also write 2=2, 80=80 or something like that. And now I can let every variable contain “AND” without worrying about if it is the first one. It’s very clever^^

“” or ” for echo

I’m still working on the gallery part of my website and almost finish it^^. I found that in the sentence of echo in PHP, ” and “” are both used in online tutorials. I read a post at the beginning which said that you can use any one for echo as you like.  But after writing so many echoes (I keep making various kinds of strange mistakes and using echo to find them-__-), I found that there are still some differences between them. For example, here is an echo in my coding:

$imageid=22

The sentence I’d like to echo is :

<a href=”mygallery/deleteworks.php?imageid=2″ onclick=”return confirm(‘Are you sure you want to delete it?’)”>Delete This Image</a>

if I use ”, I should write:

echo ‘<a href=”mygallery/deleteworks.php?imageid=’,$imageid,'” onclick=”return confirm(\’Are you sure you want to delete it?\’)”>Delete This Image</a></br>’;

if I use””, I should write:

echo “<a href=\”mygallery/deleteworks.php?imageid=$imageid\” onclick=\”return confirm(\’Are you sure you want to delete it?\’)\”>Delete This Image</a></br>”;

So when I use “” for echo, more escape characters ‘\’ are used. Since there are href and onclick in this sentence , I think using ” is more convenient. But in “”, I needn’t to take $imageid out.

But in some sentences “” is better because in PHP characters included in ‘’ is considered as a string without special meaning, like that:

$CC=22

I’d like to echo    It is 22

Using ” I should write

echo ‘It is ‘, $CC;

Using “”, I only need to write

echo “It is $CC”;

OK, that might be another general knowledge ^^

No output before SESSION

Today when I worked on the gallery part of my website,  I suddenly find something wrong with log in. Sometimes I should log in twice. I test and test, put “echo” every where, and after four hours, finally find that it is all because of one thing. NO OUTPUT BEFORE “SESSION_start()"including space and any html code. I know no output before header is permitted but I don’t know it is the same to session. OK, now I know that it is a general knowledge and I’m absolutely a newbie -____-

Storing an image as a BLOB in MySQL or just URL?

At first, I plan to store all the images in my gallery as BLOB as many online tutorial suggested to do, which is Binary Large OBject in MySQL. There are four BLOB types,  TINYBLOB, BLOB, MEDIUMBLOB, and LONGBLOB with maximum size 255 bytes, 64KB, 16MB and 4GB respectively.

But soon I found that it’s not a good idea.  In an article named “Uploading images into a MySQL database table is a bad idea“, the author said “Certain web hosts use their Terms Of Service (which no one reads) to specifically disallow uploading images into MySQL™ and will cancel the hosting account if images are stored in the MySQL™ database. Web hosts feel that image uploads place a burden on the the server that effects all users, especially if the uploaded images are large in size or they were not web-optimized before being uploaded. Check the terms of your hosting agreement before uploading images or other types of binary data.”

Maybe I should just store the URL of my images and put those images in a certain file folder. Another advantage of doing this is that it’s easier to manage those images than when changing them to BLOB.

Week8_Prac_Javascript

I keep playing with JavaScript this week because I’m working on the sign up part of my website. So the exercise of week 8 looks easier after these practices. I added a input box for the page of “How many days old you are” and now you can also input the your date of birth to calculate how many days old you are. Try it here^^

Finally finish Extra Challenge of week7 today

Today when working on my final project, I realized that a regular expression can be  used on the  telephone number validation of week7 Prac as following:

if(!preg_match(‘/^\d+$/’, $phonenumber)){  
exit(‘Telephone number can not contain characters other than numbers’);}     

Here are some other useful regular expressions

For username(5 to 16 characters, only numbers, letters and _ are permitted):^[a-zA-Z][a-zA-Z0-9_]{4,15}$

For username(only letters, no space):^\S+[a-z A-Z]$

For username(only numbers, no space):^\d+$

For password(begin with letter, 6 to 18 characters):^[a-zA-Z]\w{5,17}$

No ^%&’,;=?$\:[^%&’,;=?$\x22]+

Email:\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

For date(****-**-**):^\d{4}\-\d{1,2}-\d{1,2}$

Week7 Prac_PHP

Finished PHP exercise tonight. It’s a little tricky for me 🙂

For the Challenge Prac, I’ve add a new field named country and let its length to be 45. But just found the longest official country name used to be “al-Jamahiriyah al-‘Arabiyah al-Lïbïyah ash-Sha’biyah al-Ishtirãkiyah”, which had 57 letters. Actually, it has a much simpler official name now-Libya. 😀

I’ll try the Extra Challenge tomorrow. Good night.

Here is the link http://tingzh.net/tingzhang_DMT/prac/prac_index.html. Please click Week7_Prac.