1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to display excel or mySQL rows one by one?

Discussion in 'PHP' started by KingCobra, Jun 10, 2020.

  1. #1
    Hi friends,

    I have a excel file with about 200 rows.
    I need to display data from each row one by one (one row's data at a time).
    Suppose, one row's data will display and stay 3 seconds and then next row's data will display in the same place (previous data will disappear).
    The full process will loop continuously.

    excel file:
    excel.jpg

    here is the display example of one row:
    display.jpg
    You can use PHP/jQuery/AJAX/JavaScript etc to do this.
    You can use fading style or any other style for data transaction.

    Note: If it is hard form excel sheet, then I can convert it to mySQL.
    So you can pull data from excel or mysql, what ever better for you.
    (excel is preferable)

    Please help me doing this. I searched but did not find the solution anywhere.
    Thank you
    SEMrush
     
    KingCobra, Jun 10, 2020 IP
    SEMrush
  2. hdewantara

    hdewantara Well-Known Member

    Messages:
    485
    Likes Received:
    44
    Best Answers:
    22
    Trophy Points:
    130
    #2
    Hi.
    one untested idea is to save all of those 200 rows to a CSV file.

    On 1st page load javascript will load this CSV into an array of object:
    data = {
      country: 'united states',
      infected: 1292879,
      death: 76942,
      recovered: 217251
    }
    Code (JavaScript):
    and when loaded:
    • start showing 1st row.
    • start 3-seconds timer to show next row.
     
    hdewantara, Jun 10, 2020 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,298
    Likes Received:
    351
    Best Answers:
    15
    Trophy Points:
    235
    #3
    Try this
    Save your excel as CSV file. (data.csv)
    It will look something like this
    USA,111,11,100
    India,333,33,300

    <?php
    
    $c= file_get_contents( 'data.csv' );
    $arr=array();
    $d= explode("\n",$c);
    foreach($d as $v){
    if(strlen($v)>2){
    $v=str_replace('"', '', $v);
    $arr[]=explode(',',$v);
    } }//foreach ends
    $count=sizeof($arr);
    $arr= json_encode($arr);
    
    echo '<script type="text/javascript">
    var obj= '.$arr.';
    var max='.$count.';
    </script>';
    ?>
    
    <script type="text/javascript">
    var r=0;
    function getNextRow(){
    s=obj[r];
    str=" "+s[0]+" | "+s[1]+" | "+s[2]+" | "+s[3]+" ";
    document.getElementById("cData").innerHTML = str;
    ++r; if(r>=max){ r=0; }
    }
    </script>
    
    <p id="cData"></p>
    <script type="text/javascript">
    getNextRow();
    setInterval( getNextRow, 3000 );
    </script>
    Code (markup):
     
    Last edited by a moderator: Jun 10, 2020
    JEET, Jun 10, 2020 IP
  4. KingCobra

    KingCobra Well-Known Member

    Messages:
    282
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #4
    Hi @hdewantara
    Thank you. You gave the proper idea.
     
    Last edited: Jun 11, 2020
    KingCobra, Jun 11, 2020 IP
  5. KingCobra

    KingCobra Well-Known Member

    Messages:
    282
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #5
    Hi @JEET
    Thank you very much. It works perfectly. You did great work.
    Is it possible to do from mysql table or excel sheet?
     
    KingCobra, Jun 11, 2020 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,298
    Likes Received:
    351
    Best Answers:
    15
    Trophy Points:
    235
    #6
    @KingCobra
    You can make the PHP array from MySQL instead of from CSV file.
    Only that part of code will change.
    About reading directly from excel, I'm not sure, never did that.
    There must be some class, some package which does, but its much easier to just convert the excel into csv and use php to parse it into an array.
     
    JEET, Jun 11, 2020 IP