-1

Hi I have the Below PHP and HTML code Which Reads the CSV file and List the 4 rows of CSV file as Link in HTML.I want to make the below code as dynamic through iteration.How can I do it.As I am Novice in Php I am stuck over here.

Below is the Snippet of PHP code

<?php
    function readCSV($fileName) {
      $rows = array();
      $rows = file($fileName);
      return $rows;
}   ?>

  <span><b>Available Positions: </b></span>

  <ul>
    <li><a href="#<?php print_r(readCSV('joinUs.csv')[1]);?>"><?php print_r(readCSV('joinUs.csv')[1]);?></a></li>
    <li>|</li>
    <li><a href="#<?php print_r(readCSV('joinUs.csv')[2]);?>"><?php print_r(readCSV('joinUs.csv')[2]);?></a></li>
    <li>|</li>
    <li><a href="#<?php print_r(readCSV('joinUs.csv')[3]);?>"><?php print_r(readCSV('joinUs.csv')[3]);?></a></li>
    <li>|</li>
    <li><a href="#<?php print_r(readCSV('joinUs.csv')[4]);?>"><?php print_r(readCSV('joinUs.csv')[4]); ?></a></li>
  </ul>

joinUs.csv file

Available Position
Sr. Product Engineer
Quality Assurance Engineer
Frontend Javascript Engineer
Business Development Manager

Displayed as enter image description here

Please Help me in making the Above code Dynamic Instead for Hardcoing the arrays as 1,2,3,4 in href tag.

Thanks in Advance Rahul Jain

4
  • See the manual for how to do this with fgetcsv Commented Jun 2, 2014 at 19:12
  • possible duplicate of PHP reading a csv file effectively Commented Jun 2, 2014 at 19:13
  • No Its not Duplicate as I am facing issue with integrating with href tags Commented Jun 2, 2014 at 19:14
  • I don't think you have a grasp on what file() is returning or how to loop through the results. Commented Jun 2, 2014 at 19:17

2 Answers 2

1

https://www.php.net/fgetcsv

You should open a file (with fopen function), then you can use the fgetcsv method to fetch a line from your csv as an array. Use a loop to iterate over every line.

    $file = fopen($fileName, "r");
    
    while ($row = fgetcsv($file, 1000, ' ')) {
        echo "<ul>";
        foreach ($row as $element) {
            echo "<li>$element</li>";
        }
        echo "</ul>";
    }
    
    fclose($file);
Sign up to request clarification or add additional context in comments.

4 Comments

Hi You have told about the Iteration Part But How do I make the Html list tag dynamic <li><a href="#<?php print_r(readCSV('joinUs.csv')[1]);?>"><?php print_r(readCSV('joinUs.csv')[1]);?></a></li>
$row is an array, you can go through each of its element with a simple foreach loop (i'll edit the answer).
Hi Can I add a href tag in the echo inside forEach loop in the above mentioned code to display the link dynamically.I am new to PHP and I just want to know whether we can add Html a ref inside php.
PHP will generate the HTML you echo, so yes, you can use <a href...> or any other tag you want.
1

Read the CSV file into an array (like you're doing, but only once). Then loop through the array using foreach(). There is an example in the PHP documentation: https://www.php.net/manual/control-structures.foreach.php

4 Comments

But How to make a href tag dynamic.
Did you read the documentation on the page I linked?
Yes I have gone through it But The main Issue is in integrating with href tag and I am unable to find in it
you see the $value in all those examples? that's what you'll use instead of all that print_r(readCSV('joinUs.csv')[2]) mess that you have now

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.