22 March 2023

How to get XML data from URL in PHP?

Get XML data from URL in PHP

function get_xml_from_url($url){

  $ch = curl_init();

  curl_setopt($ch, CURLOPT_URL, $url);

  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');

  $xmlstr = curl_exec($ch);

  curl_close($ch);

  return $xmlstr;

} 

27 February 2023

How to use datatable in laravel 8 ?

Use datatable in laravel 8


<?php


/* =====In Controller file===== */

use DataTables;   


   public function ajax_get_blogs(Request $request)

    {

        if ($request->ajax()) {

            $data = Blog::select('*');

            return Datatables::of($data)

                    ->addIndexColumn()

                    ->addColumn('featured_image', function($row){


                        if($row->featured_image == ''){ $featured_image = asset("upload/image-not-found.jpg"); } else {  $featured_image =  asset("upload/" . $row->featured_image); }


                        $featured_image_view = '<img src="'. $featured_image .'">';

 

                         return $featured_image_view;

                         })


                    ->addColumn('action', function($row){


                           $btn = '<a href="javascript:void(0)" data-blog_id = ' . $row->id . ' class="edit btn btn-primary btn-sm">View</a>';

    

                            return $btn;

                    })

                    ->rawColumns(['action','featured_image'])

                    ->make(true);

        }

        

        return view('admin.blogs.blogs-list');

    }

/* =====In Blade file===== */

?>

<html>

    <head>

        <style>

            img {

                border: 1px solid red;

                width: 200px;

                height: 150px;

                object-fit: cover;

            }

            img[src=""] {

                background:url("http://localhost/laravel-testing/upload/images/blogs/build.jpg");

                object-fit: cover;

            }

        </style>

        <meta name="csrf-token" content="{{ csrf_token() }}">  

    </head>

    <body>

    <table id="blog_post_table">

        <thead>

            <tr>

                <th>No</th>

                <th>Title</th>

                <th>Description</th>

                <th>Image</th>

                <th width="100px">Action</th>

            </tr>

        </thead>

        <tbody>            

        </tbody>

    </table>

<script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>

<script type="text/javascript" charset="utf8" src="https://cdn.datatables.net/1.13.2/js/jquery.dataTables.js"></script>

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.13.2/css/jquery.dataTables.css">

<script>

    jQuery(document).ready(function($){

        $(function () {

            var table = $('#blog_post_table').DataTable({

            processing: true,

            serverSide: true,

            ajax: "{{ route('blogs.AjaxGetBlogs') }}",

            columns: [

                {data: 'id', name: 'id'},

                {data: 'title', name: 'title'},

                {data: 'description', name: 'description'},

                {data: 'featured_image', name: 'featured_image', orderable: false, searchable: false},

                {data: 'action', name: 'action', orderable: false, searchable: false},

            ]

        });


        });

    });

</script>

</body>

</html>

How to upload file in public folder by storage function in Laravel 8?

upload file in public folder by storage function in Laravel 8



//filesystems.php   


   'disks' => [

        'uploads' => [

             'driver' => 'local',

             'root' => public_path() .'/upload',

             'visibility' => 'public',

        ]

    ]

//My-project(Laravel)\public\upload\images\blogs


use Validator;

use file;

use Illuminate\Support\Facades\Storage;



$request->validate([

'featured_image' => 'required|max:2048',

]);


$name = $request->file('featured_image')->getClientOriginalName();

$file_path = $request->file('featured_image')->storeAs('images/blogs', $name, 'uploads');

How to preview doc or docx file in html/iFrame?

Preview doc or docx file in html/iFrame

var link = "paste docx file link here";
var iFrameUrl = 'https://view.officeapps.live.com/op/embed.aspx?src=' + link + '&embedded=true';

$('#documentModalIframe').attr('src',iFrameUrl);

or

<iframe src="https://docs.google.com/gview?url=http://remote.url.tld/path/to/document.doc&embedded=true"></iframe>

23 February 2023

How to Keep active navbar according to url in jQuery?

Stay active navbar according to url in jQuery


<script>

    /** add active class and stay opened when selected */

var url = window.location;


// for sidebar menu entirely but not cover treeview

$('ul.nav-sidebar a').filter(function() {

    return this.href == url;

}).addClass('active');


// for treeview

$('ul.nav-treeview a').filter(function() {

    return this.href == url;

}).parentsUntil(".nav-sidebar > .nav-treeview").addClass('menu-open').prev('a').addClass('active');

</script> 

How to change url or submit form into url without refresh page by jQuery?

Change url or submit form into url without refresh page by jQuery?
 

<html>

    <head>

    <script src="https://code.jquery.com/jquery-3.6.3.min.js" integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU=" crossorigin="anonymous"></script>

    </head>

    <body>

        <Form action="" method="get" id="search_form">

            <input type="text" name="first_name" value="demo">

            <input type="text" name="last_name" value="tester">

            <input type="text" name="city" value="sangrur">

            <input type="submit" name="submit" value="Submit">

        </Form>

    </body>

    <Script>

        jQuery("document").ready(function($){

            $('#search_form').submit(function(e){

                e.preventDefault();

                var search_form = $(this).serialize();

                var refresh = window.location.protocol + "//" + window.location.host + window.location.pathname + '?arg=1&' + search_form;    

                window.history.pushState({ path: refresh }, '', refresh);


                var current_url = window.location.href;

                var url_data = parseURLParams(current_url);

                console.log(url_data);

                console.log(url_data['first_name']);

                

            })

            function parseURLParams(url) {

                var queryStart = url.indexOf("?") + 1,

                    queryEnd   = url.indexOf("#") + 1 || url.length + 1,

                    query = url.slice(queryStart, queryEnd - 1),

                    pairs = query.replace(/\+/g, " ").split("&"),

                    parms = {}, i, n, v, nv;


                if (query === url || query === "") return;


                for (i = 0; i < pairs.length; i++) {

                    nv = pairs[i].split("=", 2);

                    n = decodeURIComponent(nv[0]);

                    v = decodeURIComponent(nv[1]);


                    if (!parms.hasOwnProperty(n)) parms[n] = [];

                    parms[n].push(nv.length === 2 ? v : null);

                }

                return parms;

            }

        });

    </Script>

</html>

How to add color according to first letter of text in PHP?

Add color according to first letter of text in PHP

<?php

$name = "jaspreet";

$initial = strtoupper(substr($name, 0, 1)); // Get the first letter and convert it to uppercase

$color = '#' . substr(md5($initial), 0, 6); // Generate a 6-digit hexadecimal color code from the MD5 hash of the initial

echo "<div style='background-color: $color;'>$initial</div>"; // Display the initial inside a div element with the generated color as the background color

?> 

How to fetch instagram posts data?

Fetch Instagram posts data by pasting this code in browser console. (() => { /* ====================================================...