Eiffel-Mtl Posted September 19, 2022 Share Posted September 19, 2022 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 Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/ Share on other sites More sharing options...
ginerjm Posted September 19, 2022 Share Posted September 19, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600772 Share on other sites More sharing options...
Eiffel-Mtl Posted September 19, 2022 Author Share Posted September 19, 2022 The href is pointing to <span>History</span> {{{text from line 1 cell 1 of data files}} <span>{{{text from line 1 cell two of data files}}</span> from lightbox. The multiple braces id only for parameter Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600774 Share on other sites More sharing options...
ginerjm Posted September 19, 2022 Share Posted September 19, 2022 lightbox? You have an anchor pointing to something on your web page and you want to jump to that? Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600775 Share on other sites More sharing options...
Eiffel-Mtl Posted September 19, 2022 Author Share Posted September 19, 2022 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. Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600776 Share on other sites More sharing options...
ginerjm Posted September 19, 2022 Share Posted September 19, 2022 So you need to open that file while your script is building the page and it's html and read that line and put it into your html. Is that not easy? Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600785 Share on other sites More sharing options...
Eiffel-Mtl Posted September 19, 2022 Author Share Posted September 19, 2022 Iit is maybe easy, but it's been long time that I did code in php. Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600788 Share on other sites More sharing options...
maxxd Posted September 19, 2022 Share Posted September 19, 2022 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600790 Share on other sites More sharing options...
Eiffel-Mtl Posted September 19, 2022 Author Share Posted September 19, 2022 (edited) 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 September 19, 2022 by Eiffel-Mtl Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600791 Share on other sites More sharing options...
ginerjm Posted September 19, 2022 Share Posted September 19, 2022 Have no idea what you are thinking of when you posted this. Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600795 Share on other sites More sharing options...
Barand Posted September 19, 2022 Share Posted September 19, 2022 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> Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600798 Share on other sites More sharing options...
maxxd Posted September 19, 2022 Share Posted September 19, 2022 OK, so places to start researching in order to understand Barand's reply: https://www.php.net/manual/en/book.pdo.php https://www.php.net/manual/en/ref.pdo-mysql.php https://www.lifewire.com/database-normalization-basics-1019735 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. Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600799 Share on other sites More sharing options...
cyberRobot Posted September 20, 2022 Share Posted September 20, 2022 In case you're interested, and you're using HTML5, the <details> tag works like an accordion box...and doesn't require JavaScript. More information (including examples and browser support info) can be found here:https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details Quote Link to comment https://forums.phpfreaks.com/topic/315349-data-from-database-or-files/#findComment-1600849 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.