Jump to content

Data from database or files


Eiffel-Mtl
 Share

Recommended Posts

Hi,

it's been a long time since I've coded in php and I've lost a bit.

I want to write a website with php and I want to use data from either mysql, csv files or excel file and I want to know how to call the data.

Here is a bit of my code

 

<a href="#boatsLightbox{{call first line of data files}}" class="myButton inline">
                  More info...
                </a>

<span>History</span>
{{{text from line 1 cell 1 of data files}}
<span>{{{text from line 1 cell two of data files}}</span>

I want to do the same the each line (I will have 5 lines). Manually call the each line and data from each line when I call it.

Thank you

Link to comment
Share on other sites

I must be a little rusty as well since I don't know what the href is pointing to.  Nor what the contents of the 'more info...' block embedded in that anchor tag.  Nor why the multiple braces are meant to do.   Certainly a different method of coding that I haven' run across yet.

Link to comment
Share on other sites

The content <span>History</span> {{{text from line 1 cell 1 of data files}} <span>{{{text from line 1 cell two of data files}}</span> is hidden. The a href is open it from a lightbox (https://www.w3schools.com/howto/howto_js_lightbox.asp).

But I need to set the data for this <span>History</span> {{{text from line 1 cell 1 of data files}} <span>{{{text from line 1 cell two of data files}}</span> from a data file. So when the lightbox is oppening it it will depend of the line of the data file.

Link to comment
Share on other sites

Problem is, you're talking about very different ways of getting the data. In the grand scheme of things the problem is as easy as ginerjm says - you get the data and output it while building the response. However, if you're using a CSV you'll need to research fopen() and fgetcsv(). If you're using a JSON string it'll be fopen() and decode_json(). If it's a database, research PDO and MySQL. And if it's an Excel file, that's a whole other can of worms that's going to entail third-party libraries.

Each potential path has it's own ups and downs; you'll need to decide how you want to store and retrieve the data. Outputting said data is easy - in every case it's a simple echo statement.

  • Like 1
Link to comment
Share on other sites

Ok, let say from a mysql database.

From database:

History text will be instead of "abcdefg"

    <div class="accordion-title"><span>History</span></div>
    <div class="accordion-content">abcdefg</div> 

Description text will be instead of "hijklm"

    <div class="accordion-title"><span>Description</span></div>
    <div class="accordion-content">hijklm</div> 

And so on.

boats.txt

Edited by Eiffel-Mtl
Link to comment
Share on other sites

If you take the MySQL route then use PDO to interfaace with DB. Create a connection file which can be included in your scripts.

Mine is

<?php
#------------------------------------------------------------------------------------------+
#                                    db_connect.php                                        |
#------------------------------------------------------------------------------------------+
const HOST     = 'localhost';                                                            
const USERNAME = '????';                                                                 
const PASSWORD = '????';                                                                 
const DATABASE = '????';               // default db                                     
                                                                                         
function pdoConnect($dbname=DATABASE)                                                    
{                                                                                        
    $db = new PDO("mysql:host=".HOST.";dbname=$dbname;charset=utf8",USERNAME,PASSWORD);  
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);                        
    $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);                   
    $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);                                
    return $db;                                                                          
}

Then the code you need will look similar to this

<?php
require 'db_connect.php';
$pdo = pdoConnect();

$res = $pdo->query("SELECT history
                         , description
                    FROM boats     
                  ");

$output = '';                  
foreach ($res as $row) {
    $output .= "<span>History</span><br>
                {$row['history']}<br>
                <span>{$row['description']}</span><br>
                <br>
               ";
}
?>
<html>
<body>
    <?= $output ?>
</body>
</html>

 

Link to comment
Share on other sites

OK, so places to start researching in order to understand Barand's reply:

Full disclosure, I've not read the final link fully but from what I've scanned it looks decent. Your data structure looks simple enough right now, just know that as it grows the need for normalization will grow.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.