Pagination example

PHP and HTML

<ul class="pagination">
    <li><a href="javascript:void(0);" class="pbutton"></a></li>
<?php 
        if(!empty($_POST['selected'])){$page = $_POST['selected'];}else{$page=1;} //selected page
    $last=6; //number of pages ( total number of articles / number of shown articles on page )
    $top = $page+2; 
    $bottom = $page-2;
    for($x=$bottom;$x<$top;$x++){
        if($top>$last){$top=$last;}
        if($x<=0){$bottom+=1;$top+=1;}                              
    }
                
    for($i=$bottom;$i<=$top;$i++){ ?>
           <li class="<?php echo ($page==$i)?"active":"";?>"><a href="javascript:void(0);" class="pbutton"><?=$i;?></a></li>
<?php 
    }
                        
    if($page!=$last){ ?>
        <li><a href="javascript:void(0);" class="pbutton last_page" id="<?=$last?>" ></a></li>
    <?php } ?>
                            
</ul>

jQuery

$(document).on("click",".pbutton",function() {
        var selected = this.text; //Selected page
        if(selected==""){selected = 1;} //first rows
        if(selected==""){selected = $(".last_page").attr("id");} //last rows
        
        $.ajax({
          url:'admin.php',
          type:'POST',
          data:{ 
            command:"show_regular_quotation", //name of var: value of var  
            selected:selected
          }  
         }).done(function(data){
            $(".content").html(data);
        });     
    });

The code is perfectly compatible with bootstrap.

Currency convertor with PHP

function currency_convert($amount, $from, $to){
    $result = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$from&to=$to");
    $result = explode("<span class=bld>",$result);
    $result = explode("</span>",$result[1]);  
    $converted_amount = preg_replace("/[^0-9\.]/", null, $result[0]);
    echo $converted_amount;
}

currency_convert(1,'gbp','usd');

Function for getting random word from words in brackets

function randomWords($paragraph){ //get one from the words in {}
        preg_match_all('/{(.*?)}/s', $paragraph, $matches);
        foreach($matches[1] as $variations){
            $variations_array = explode('|',$variations);
            $paragraph = str_ireplace('{'.$variations.'}',$variations_array[array_rand($variations_array)],$paragraph);
        }
        return $paragraph;
    }

For example {traces | trails | spots}. Will return one of the words in brackets.

PHP function for downloading images from http

This function will download images from another site , resize them and put suffix (if needed).

function download_resized_jpeg($filename,$width,$height,$suffix){
    $name = explode('/',$filename);
    $newfilename = end($name);

    // Content type
    //header('Content-Type: image/jpeg');

    list($width_orig, $height_orig, $type) = getimagesize($filename);

    $ratio_orig = $width_orig/$height_orig;

    if ($width/$height > $ratio_orig) {
       $width = $height*$ratio_orig;
    } else {
       $height = $width/$ratio_orig;
    }

    // Resample
    $image_p = imagecreatetruecolor($width, $height);
    if ($type == 1)
    {
        $image = imagecreatefromgif($filename);
    }
    elseif ($type == 2)
    {
        $image = imagecreatefromjpeg($filename);
    }
    elseif ($type == 3)
    {
        $image = imagecreatefrompng($filename);
    }
    else
    {
        $image = imagecreatefromwbmp($filename);
    }
    
    if($type==1 or $type==3) //this code below should preserve transparency but I couldn't try it out for now...
    {
        imagealphablending($image_p, false);
        imagesavealpha($image_p, true);
        imagecolortransparent($image_p, imagecolorallocatealpha($image_p, 0, 0, 0, 127));
    }
    
    imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);

    // Output
    if ($type == 1)
        imagegif ($image_p, "../pictures/$newfilename$suffix".".gif", 100);
    elseif ($type == 2)
        imagejpeg($image_p, "../pictures/$newfilename$suffix".".jpg", 100);
    elseif ($type == 3)
        imagepng ($image_p, "../pictures/$newfilename$suffix".".png", 0);
    else
        imagewbmp ($image_p, "../pictures/$newfilename$suffix".".bmp", 100);
}

Cross domain access to data on the server PHP

If you are doing a ajax call to retrieve data from your own server on a platform which is set to prevent these ajax calls . To bypass “Access-Control-Allow-Origin” you need to allow cross domain access by including this in your server php code.

header('Access-Control-Allow-Origin: *');

OR – for only one domain

header('Access-Control-Allow-Origin: http//your-domain.com');

I hope this will help you gain access to you cross-domain data.

Display Recent Posts outside the WordPress

How to import recent post from your internal blog to your website without any difficulties.
First you need to create wp-latest-blogs.php file where you include wp-load.php for your wp blog.

<?php
    /**
    * WoredPress Latest blog entries
    * @package WordPress
    */
    require( './blog/wp-load.php' );
    // Load the recent top 10 posts
    query_posts( 'posts_per_page=10' );
    ?>
    <div id="blogPosts">
    <ul id="blogList">
    <?php if (have_posts()) : ?>
    <?php while (have_posts()) : the_post(); ?>
    <li>
    <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
    <?php the_title(); ?><span class="date"><?php the_time('F jS, Y') ?></span></a>
    </li>
    <?php endwhile; ?>
    <?php endif; ?>
    </ul>
    </div>

Continue reading “Display Recent Posts outside the WordPress”