Translate

Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Monday, May 11, 2015

How to Delete an element from an array?





In this post, we will answer the question : How to Delete an element from an array?

First of all , we  will create our own array called :$aFruits


  
   $aFruits = array('apple','lemon','grapes','pineapple');
 

Secondly,  if we want to delete the lemon element, we have to use the function called unset and we need to access to  that element by  its  position number.


  
  unset($aFruits[1]);
 

Ouput:
 Array ( [0] => apple [2] => grapes [3] => pineapple )

Friday, November 7, 2014

How to remove the letter of an alphanumeric string


How to remove the letter of an alphanumeric string



In this post, I'll  try to explain how to remove the letter of an alphanumerics string.Maybe this tutorial could be helpful for you :

we have the following code:



  $sMystring = 'Tra789asdf'; //1
  $pMypattern = '/[a-zA-z]/'; //2
  $sMyreplacement = ''; //3
  echo preg_replace($pMypattern, $sMyreplacement, $sMystring);//4
 

Code explanation:

First Line: We have our alphanumeric string ,I mean a combination between numbers and letters.

Second Line: We have out REGEX pattern, which identify the letter in our string $pMypattern.

Third  Line: We have a variable that will use to replace the letter, by that I mean instead of letter we  will have in the string blank spaces.

Fourth Line:  We will have our output and it is important to note the preg_replace
 which  identified in the string the pattern and remove the matches and replace with the new value in our case is: $Myreplacement.


Output : 789

If want the opposite output, I mean remove the number the code will be:
   

    $sMystring = 'Tra789asdf';//1
    $pMypattern = '/[0-9]/';//2
    $sMyreplacement = ''; //3
    echo preg_replace($pMypattern, $sMyreplacement, $sMystring); //4
 

Output :: Traasdf


Saturday, February 8, 2014

Conectarse a una Base De Datos MYSQL con PHP

Como Conectarse a una Base De Datos MYSQL  con PHP 

Para conectarse a una base de datos mysql tienes que hacer los siguientes:


Primero establecemos la coneccion con la funcion de php  "
mysql_connect
" el cual recibe como parametro el server,usuario y el password y despues seleccionamos la base de datos que queremos con la funcion 
mysql_select_db la cual recibe como parametro el nombre de la base y la coneccion.


<?PHP
//en  mi local como no posee password lo dejo en blanco pero en produccion tu tendras que //colocar el password
$link= mysql_connect("localhost","root",""); 


//seleccionamos el nombre de la base como por ejemplo bda_test
$db_selected = mysql_select_db('bda_test', $link);
?>

y con esto ya  estas conectado la base de datos  de mysql y podras consultar la base atravez querys con la funcion php mysql_query.

espero que les haya servido nos  vemos al rato.

OPERACIONES CON FECHAS EN PHP

SUMAR DIAS,MESES Y AÑOS CON PHP


Este dia tratare de explicar como sumar dias, mese y años con php.

bueno lo primero que vamos a hacer es guardar en una variable la fecha actual con la cual vamos a trabajar para ellos vamos a hacer lo siguiente:

$currentDat = date('Y-m-d',time());

una vez tengamos la fecha actual procedemos a sumar los dias,mese y años de la siguiente forma:


echo date('Y-m-d',strtotime($currentDat."+1 days"));//para sumar un dia
echo date('Y-m-d',strtotime($currentDat."+1 month"));// para sumar 1 mes
echo date('Y-m-d',strtotime($currentDat."+1 years")); //para sumar 1 año

estas serian las  salidas

para fecha actual:  2013-02-13
Sumado un dia:    2013-02-14
sumado un mes:  2013-03-13
sumado un año:  2014-02-13

Remove Duplicate array elements and sort their indexes



In this post will learn how to remove the duplicate  element from an array using PHP

1- This is our unidimensional array:

   
    $DayofWeek = array('lunes','martes','lunes','miercoles','miercoles','jueves','martes','viernes','miercoles');
 
Output:
 Array
(
    [0] => lunes
    [1] => martes
    [2] => lunes
    [3] => miercoles
    [4] => miercoles
    [5] => jueves
    [6] => martes
    [7] => viernes
    [8] => miercoles
)

As you can see we have a duplicate elements to remove the duplicity we can use the php function called array_unique() as follows:

   
    $newArray = array_unique($DayofWeek);
 



Remember  array_unique function will create a new array.

This is the output:

Array
(
    [0] => lunes
    [1] => martes
    [3] => miercoles
    [5] => jueves
    [7] => viernes
)
 
 
If you have noticed the array is untidy  if you want to sort by the indix , there is a php a php function called sort :

   
   sort($newArray);
 




This is the output
Array
(
    [0] => jueves
    [1] => lunes
    [2] => martes
    [3] => miercoles
    [4] => viernes
)

How to pass PHP variable to Javascript

   How to pass PHP variable to JavaScript



In this post we'll learn the best way  to pass PHP variable to javascript,It is really simple and easy. The example below will show you how to do it.

This is our PHP  code where we have assigned the value "Australia" to the variable $mycountry.



    $mycountry = 'Australia';
    
 


and this is our Javascript Code

<script>


  //solo imprimimos con un echo el contenido de la variable de php asignandola a una variable JS

var MynewVAr = '';


alert(MynewVAr);
 


</script>


This is quite easy this is the whole code:

<?php  


 $mycountry = 'Australia';
 

?>

<script>


  //solo imprimimos con un echo el contenido de la variable de php asignandola a una variable JS

var MynewVAr = '';


alert(MynewVAr);
 


</script>




How to print an array using PHP


 In this post, we will learn the different ways to print an array using PHP.

1- To print an array, we can use a PHP function called print_r(),which receive an array as parameter::
´
Example: we are going to create our unidimensional array

    

    $aArray = array('lunes','martes','miercoles','jueves','viernes');
 


Now, we will pass the array to the print_r function:

   
     print_r($aArray );
 

This is the output:

     Array ( [0] => lunes [1] => martes [2] => miercoles [3] => jueves [4] => viernes )




2- the second way is using the php  function var_dump()

Example: we are going to create our unidimensional array

  
$aArray = array('lunes','martes','miercoles','jueves','viernes');
 


Now, we will pass the array to the var_dump function:
  

var_dump($aArray );
 


This is the output:

      array(5) { [0]=> string(5) "lunes" [1]=> string(6) "martes" [2]=> string(9) "miercoles"   [3]=> string(6) "jueves" [4]=> string(7) "viernes" }





3-
3- Finally, we have a combination of HTML and PHP, that will help us to see the array  more  tidy:

This is the code:


<?php

   
  
 $aArray = array('lunes','martes','miercoles','jueves','viernes');
 
  ?><pre><? print_r($aArray );?></pre><?  
?>

 This is the output:


Array
(
    [0] => lunes
    [1] => martes
    [2] => miercoles
    [3] => jueves
    [4] => viernes
)


How to install Joomla

How to install Joomla