Home > Enterprise >  How to align tuples by a specific character in php and html
How to align tuples by a specific character in php and html

Time:11-21

So I'm trying to have all of my data line up by | I currently have

movieID:001 | Movie Name:Avengers Endgame | Movie Release Date 2019-04-26 | Movie Rating: 8
movieID:002 | Movie Name:Spider-Man | Movie Release Date 2002-05-03 | Movie Rating: 7
movieID:003 | Movie Name:Iron Man 1 | Movie Release Date 2008-05-02 | Movie Rating: 8
movieID:004 | Movie Name:A Bugs Life | Movie Release Date 1998-11-25 | Movie Rating: 7
movieID:005 | Movie Name:Aladdin | Movie Release Date 1992-11-25 | Movie Rating: 8
movieID:006 | Movie Name:Star Wars The Force Awakens | Movie Release Date 2015-12-18 | Movie Rating: 8
movieID:007 | Movie Name:Sonic the Hedgehog | Movie Release Date 2020-02-14 | Movie Rating: 7
movieID:008 | Movie Name:Interstellar | Movie Release Date 2014-11-07 | Movie Rating: 9
movieID:009 | Movie Name:Black Adam | Movie Release Date 2022-10-21 | Movie Rating: 7
movieID:010 | Movie Name:Joker | Movie Release Date 2019-10-04 | Movie Rating: 8

but I need :

movieID:001 | Movie Name:Avengers Endgame            | Movie Release Date 2019-04-26 | Movie Rating: 8
movieID:002 | Movie Name:Spider-Man                  | Movie Release Date 2002-05-03 | Movie Rating: 7
movieID:003 | Movie Name:Iron Man 1                  | Movie Release Date 2008-05-02 | Movie Rating: 8
movieID:004 | Movie Name:A Bugs Life                 | Movie Release Date 1998-11-25 | Movie Rating: 7
movieID:005 | Movie Name:Aladdin                     | Movie Release Date 1992-11-25 | Movie Rating: 8
movieID:006 | Movie Name:Star Wars The Force Awakens | Movie Release Date 2015-12-18 | Movie Rating: 8
movieID:007 | Movie Name:Sonic the Hedgehog          | Movie Release Date 2020-02-14 | Movie Rating: 7
movieID:008 | Movie Name:Interstellar                | Movie Release Date 2014-11-07 | Movie Rating: 9
movieID:009 | Movie Name:Black Adam                  | Movie Release Date 2022-10-21 | Movie Rating: 7
movieID:010 | Movie Name:Joker                       | Movie Release Date 2019-10-04 | Movie Rating: 8

This is the code that I have right now:

the php file:

$conn = mysqli_connect($servername, $username, $password, $dbname);

if(!$conn){
    die("Connection failed:" .mysqli_connect_error());}

$sql = "select * from USERTOMOVIE";
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        echo "userID:".$row["userID"]." | movieID:".$row["movieID"]. " | user rating ".$row["rating"]."<br>";
    }
}else{
    echo "0 results";
}

I'm pulling all the data that fills up the table from an sql file that is also running on the server alongside it.

I've tried googling my problem, but haven't been able to find any solutions so far. If anyone could provide any insight or point me in the right direction that would be great. Thank you!

CodePudding user response:

FYI: The code you posted does not match the output you posted.

I use sprintf for this.

echo sprintf('userId: %-3s | movieID: %-3s | %-20s<br>', $row["userID"],$row["movieID"], $row["rating"]);

You can adjust the numbers in the sprintf format string until you get it right.

  • Related