0

Hi im a begginer in php and html i would like to ask how do i put this table into my html sidelist. when i put my table inside that space this always appear ( "; foreach($testaroni as $x=>$y) { echo ""; echo "" . $y . ""; } echo ""; echo ""; ?>) This is my table. This is the problem (2)

<?php
session_start();
$testaroni = explode("', '" , $_SESSION["data"]);
?>

<?php
echo "<table border = '1'>";
foreach($testaroni as $x=>$y) {
echo "<tr>";
echo "<td>" . $y . "</td>";
}
echo "</tr>";
echo "</table>";
?>   

And i would like to put it inside here.

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.links line {
stroke: #999;
stroke-opacity: 0.6;
}

.nodes circle {
stroke: #fff;
stroke-width: 1.5px;
}

text {
font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
font-size: 10px;
}


</style>



<?php
session_start();
$testaroni = explode("', '" , $_SESSION["data"]);
?>

<html>
<head>
<title>Search</title>
</head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<body>

<!-- Sidebar -->
<div class="w3-sidebar w3-blue-grey w3-bar-block " style="width:15%">
<h4 class="w3-bar-item">Sidelist</h4> 


<table border = "1">
<?php
foreach($testaroni as $x=>$y) {
echo '<tr>';

echo '<td>' . $y . '</td>';
}
echo '</tr>';

?>   
</table>

</div>
</form>
</body>
</html>
Best Jeanist
  • 1,109
  • 4
  • 14
  • 34

4 Answers4

1

You just need to modify your explode function and your code will work as you want

session_start();
//lets say your $_SESSION["data"] contains
$_SESSION["data"] = 'Value1,Value2,Value3';
//$testaroni = explode("', '" , $_SESSION["data"]);// just use single or double quotes without space
$testaroni = explode("," , $_SESSION["data"]);
Sajjad Ali
  • 304
  • 3
  • 12
0

You try var_dump( $_SESSION["data"]) and you should check empty $_SESSION["data"].I try put

$testaroni = array("1","2","3");//it work fine

Hưng hoàng
  • 360
  • 2
  • 5
  • 18
  • testarr.php:3:string 'firewall', 'java', 'c++', 'sybase', 'marketing', 'chemistry', 'sql', 'unix shell', 'general programming', 'javascript', 'xml', 'j2ee' (length=132) this is what it showed – Best Jeanist May 03 '18 at 02:58
  • sorry, i can't help you . But i see you write residual tag and I think you should set Sesson is JSON type, it will be better – Hưng hoàng May 03 '18 at 03:22
0

You can read more about my this here

Seems like my eyes are failing me took me a while to see it.

You shouldn't use " when trying to use HTML tags in PHP

What you need to use is '.

<table border = "1">
<tr>
<?php
foreach($testaroni as $x=>$y) {
     echo '<td>'.$y.'</td>';
}
?>
</tr>
</table>

The reason for this is that " will treat anything inside it as string while ' will treat it as is.

Refer here for a better explanation.

Also, follow the answer above for your session since it's also wrong.

hungrykoala
  • 1,083
  • 1
  • 13
  • 28
  • so i only have to change this part echo ''.$y.'';? and the rest is ok? – Best Jeanist May 03 '18 at 02:59
  • FWIW - simply a matter of style, but the quotes issue can be avoided by using this format when outputting html - ` = $y ?>` (This trivial example doesn't do it justice, but with more html it starts to look much clearer than echoes and quotes all over the place) – Tim Morton May 03 '18 at 03:00
  • Yes, or you can stick with your template and just change `"` to `'`. Tim comment is worth noting as well it's cleaner and avoid a lot of echoes :) – hungrykoala May 03 '18 at 03:03
  • i have changed it but it still doesnt work i have edited the code ^ – Best Jeanist May 03 '18 at 03:07
  • just above the `foreach` add this `echo "hello"; die;` and tell me the result – hungrykoala May 03 '18 at 04:05
0

Your session variable is a CSV string. simply exploding on the comma isn't going to get what you're looking for. You can verify this with var_dump($testaroni);.

Use str_getcsv.

<?php
// always start with php logic; 
// it makes the script much easier to follow and 
// if you need to redirect, you haven't put out any output yet.

session_start();

// check the docs for info on this function
$testaroni = str_getcsv($_SESSION['data']);

// then, when everything is calculated, print out your output
?>
<html>
    <head>
      <title>Search</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
      <style>

      .links line {
      stroke: #999;
      stroke-opacity: 0.6;
      }

      .nodes circle {
      stroke: #fff;
      stroke-width: 1.5px;
      }

      text {
      font-family: "Lucida Sans Unicode", "Lucida Grande", sans-serif;
      font-size: 10px;
      }


      </style>
    </head>
    <body>

      <!-- Sidebar -->
      <div class="w3-sidebar w3-blue-grey w3-bar-block " style="width:15%">
        <h4 class="w3-bar-item">Sidelist</h4> 
        <table border = "1">
        <?php foreach($testaroni as $y): ?>
          <tr>
            <td><?= $y ?></td>
          </tr>
        <?php endforeach; ?>
        </table>
      </div>
    </form>
    </body>
</html>
Tim Morton
  • 2,614
  • 1
  • 15
  • 23
  • I didn't pay any attention to the css styling and inline styling. I don't know what the w3schools stylesheet does. Do you at least get the list of items in your session variable? Can't help any more tonight, but you can debug by simply using `var_dump` to see if a variable is what you expect. – Tim Morton May 03 '18 at 03:59
  • yeah i created a test table on another page it works the problem is just getting the table inside that sidebar – Best Jeanist May 03 '18 at 04:04