Remove last character from a string php

Use this code:

$string = ‘You rock’;

$string2 = substr_replace($string ,”",-1);

echo ”.$string2.”;

It will just remove the “k” letter from your string.

Be the first to comment - What do you think?  Posted by admin - June 19, 2010 at 10:45 am

Categories: Beginner   Tags:

Diacritics and php how to solve this issue

Well this can become a storage of resources so here is how to deal with diacritics:

If you have a text with diacritics and want to do with str_replace on the text you can, but when getting the text from a sql db they will still be in diacritics even if you apply the same replace to them, so how to do it?

Add this code before your “select *

mysql_query(“SET NAMES utf8″);

That’s it, now the text will be with normal diacritics and can be replaced with str_replace.

Be the first to comment - What do you think?  Posted by admin - June 18, 2010 at 9:18 am

Categories: Info   Tags:

Combining i++ with a sql statement

Let`s say you want to make a counting at your resulted sql entries like:

$SQL = mysql_query(“SELECT * FROM table”, $connection);

$i = 1;

while ($r = mysql_fetch_array))

{

echo”.$r['entry'].’, number ‘.$i.’

$i++;

}

If you have 2 entries in your db you will get in echo:

entry 1, number 1 and entry 2, number 2.

Meaning you don`t need while for $i++ and other while for the SQL SELECT * FROM

Be the first to comment - What do you think?  Posted by admin - April 14, 2010 at 8:54 pm

Categories: Beginner   Tags:

How to count characters from a string?

Is very easy:

Use strlen,

example:

$s = ‘Monica has an apple’;

$count = strlen($s);

echo”.$count.”;

So it will result a number, meaning how many character has that string.

It can be useful if you want to create something like a [...] to an article making with an if:

if ($count > 130)

{

echo’[...]‘;

}

else

{

echo”;

}

so if the article has less than 130 characters you should not have a [...] at its end.

Be the first to comment - What do you think?  Posted by admin - April 13, 2010 at 10:26 pm

Categories: Beginner   Tags:

Creating a sql database

So you are in phpmyadmin.

Just create a table with 4 rows.

id – stands for the number of the entry – set it to int

name – varchar – 255 max characters

text – text – about 5000 or more characters.

That is all, you got your sql database.

Be the first to comment - What do you think?  Posted by admin - March 10, 2010 at 8:35 pm

Categories: Beginner   Tags:

Config file php sql

To make the connection with the sql database you need a config.php

What should contain:

<?
$dbadress = “localhost”;
$dbname = “——–”;
$dbuser = “————–”;
$dbpass =”————–”;
$connection = mysql_connect($dbadress, $dbuser, $dbpass) or die (“Cannot connect to MySQL!”);
mysql_select_db($dbname, $connection) or die (“I cannot find the db!”);
?>

Make this

<? include (‘config.php’); ?>

every time you want to do something to the sql database, select, delete, update, etc.

Be the first to comment - What do you think?  Posted by admin - at 8:33 pm

Categories: Beginner   Tags:

Website structure by php

The best way to manage a website structure is to make it with php include.

So you can change a header and that change to take effect for all the webpages of the website.

Usually you need:

Header

Main content

Right side / left side (optional)

Footer.

Create these 5 / 3 pages and add in index.php

<?

include (‘header.php’);

include (‘maincontent.php’);

include (‘footer.php’);

?>

Also left and right side if you got those.

Be the first to comment - What do you think?  Posted by admin - at 8:29 pm

Categories: Tutorials   Tags:

OOP coding

Or object oriented programing.

An easy way to make a webpage easy to manage either by the coder or the designer.

All the code will just stay in one php page, and those codes will be used by a line of code.

You can create functions in that page as many as you want.

Be the first to comment - What do you think?  Posted by admin - at 8:26 pm

Categories: Info   Tags:

Php to Pdf

An ideea was to generate an image in php and add that image into a pdf using fpdf from fpdf.org.

Other way is to pass all the data and code into the pdf wich is the best way but the hardest way.

Visit fpdf.org and download their miniscript, also there are lots of tutorials there that will make you understand how to convert php to pdf.

With the image was only an ideea, after I would create that script expect that this post to contain it.

Be the first to comment - What do you think?  Posted by admin - at 8:22 pm

Categories: Info   Tags:

Making a contact form in php

Create a table in sql.

id – int

name = varchar

phone = varchar

email = varchar

city = varchar

So we can get the name, email, phone and city of the person who will send us a message.

<form name=”contact” action=”contactproc.php”>

<input name=”name” type=”text” value=”">

….

add for phone email and city the same line as above only changing the name of the input then

<input name=”submit” type=”submit” value=”Send message”>

</form>

Add a second php file called contactproc.

<?

$name = $_POST['name'];

….

same for city, phone, email.

$insert = “INSERT INTO contact (`name`, `phone`, `email`, `city`) VALUES (‘$name’, ‘$phone’, ‘$email’, ‘$city’);”;

mysq_query($insert);

?>

Be the first to comment - What do you think?  Posted by admin - at 7:49 pm

Categories: Tutorials   Tags:

Next Page »