I have a string that contains multiple lines. how can I convert it into tables where in each td value is every word of each line.
example of the string.
Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T
Eth1/2 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T
expected table outcome:
Eth1/1 | VPC_PEER_KEEPALIVE | connected | routed | full | 1000 | 1000base-T
Eth1/2 | VPC_PEER_KEEPALIVE | connected | routed | full | 1000 | 1000base-T
<?php
$str = "Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T
Eth1/2 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T";
// echo $str;
$v1 = explode(PHP_EOL, $str);
foreach ($v1 as $key => $value) {
$v2 = explode(" ", $value);
echo "<tr>";
foreach ($v2 as $key2 => $value2) {
echo "<td>" .$value2 ."</td>";
}
echo "</tr>";
}
?>
This JS code will work-
var string = "Eth1/1 VPC_PEER_KEEPALIVE connected routed full 1000 1000base-T"; //Whatever your string is
string = string.split(" ");
var table = "<table id='table' border='1px'><tr>";
for (i = 0; i < string.length; i++) {
table += "<td>" + string[i] + "</td>";
}
table += "</tr></table>";
document.write(table); //Whatever you want do with your table
Related
I have managed to create two arrays from database, one for table_headers as follows
$tb_headers=array('010','011','012','013','014','015','016');
And also generated other arrays of table _data from database as follows
$tb_data=array(
array('011','013','014','015'),
array('010','012','014','015','016'),
array('010','011','013','016'),
array('010','011','012','013','014','015','016')
);
How do I generate table of this format??
th 010 | 011 | 012| 013| 014 | 015 | 016
row1 - | 011 | - | 013| 014 | 015 | -
row2 010 | - | 012 | - | 014 | 015 | 016
row3 010 | 011 | - | 013| - | - | 016
row4 010 | 011 | 012 | 013| 014 | 015 | 016
I have tried to write this script which is not working as expected
<table style="width:50%;" border="1">
<tr>
<?php
foreach ($tb_headers as $key => $value) {
echo '<th>'.$value.'</th>';
}
echo '</tr>';
foreach ($tb_headers as $key => $data) {
echo '<tr>';
if(!empty($tb_data[$key])&& $tb_data[$key]==$data ){
echo '<td>'.$tb_data[$key].'</td>';
}else{
echo '<td>-</td>';
}
echo '</tr>';
}
?>
</table>
Try this, which uses the php in_array() function.
<table>
<tr>
<?php
// header
foreach ($tb_headers as $value) echo '<th>' . $value . '</th>';
?>
</tr>
<?php
// content
foreach ($tb_data as $line => $values) {
echo '<tr>';
foreach ($tb_headers as $header) {
echo '<td>';
if (in_array($header, $values)) echo $header;
else echo '-';
echo '</td>';
}
echo '</tr>';
}
?>
</table>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I'm making a hangman game.
Everything works somewhat fine untill one last part.
I want to show the letters he managed to guess correctly.
This works somewhat but only after the 2nd guess.
But that's not the main problem i'm struggling with.
When a user actually guesses a correct letter it shows it in the secret word.
(secret word meaning the word the user has to guess but in dash lines)
But when the user guesses another time, the previous letter dissapears out of the secret word.
The last thing i'm struggling on is that when there are multiple letters in the same word the loops stop at the first one it finds.
(excuse me for my bad scripting. I'm fairly new to this thanks!)
(also excuse my Dutch)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Hangman Back end php</title>
<h1> Hangman </h1>
<h3>Raad een letter van het te raden woord</h3>
</head>
<body>
<?php
$hang[0] =
' -------
|/ |
|
|
|
|
|
/|\
-------------';
$hang[1] =
' -------
|/ |
| o
|
|
|
|
/|\
-------------';
$hang[2] =
' -------
|/ |
| o
| |
| |
|
|
/|\
-------------';
$hang[3] =
' -------
|/ |
| o
| |
| |
| /
|
/|\
-------------';
$hang[4] =
' -------
|/ |
| o
| |
| |
| / \
|
/|\
-------------';
$hang[5] =
' -------
|/ |
| o
| --|
| |
| / \
|
/|\
-------------';
$hang[6] =
' -------
|/ |
| o
| --|--
| |
| / \
|
/|\
-------------';
function HasBeenUsed($allLettersGuessed, $letter){
if ($allLettersGuessed =="") {
#No letter guessed yet
return false;
}
else {
$hasLetter = false;
#$length = strlen($allLettersGuessed);
$hasLetter = strpos($allLettersGuessed, $letter) !== false;
if ($hasLetter) {
$hasLetter = true;
return $hasLetter;
}
else {
$hasLetter = false;
return $hasLetter;
}
}
}
function Restart()
{
$wrongGuess = 0;
return array($oldLetterGuessed, $wrongGuess);
}
$solution = "DANSEN";
if (isset($_GET['letterGuessed'])) {
$currentGuess = $_GET['letterGuessed'];
# Check the letter
if (HasBeenUsed($solution, $currentGuess) == false) {
echo "<b> Wrong </b>";
$wrongGuess = $_GET['wrongGuess'] + 1;
echo "<pre>" . $hang[$wrongGuess] . "</pre>";
}
else {
$wrongGuess = $_GET['wrongGuess'];
echo "<pre>" . $hang[$wrongGuess] . "</pre>";
}
}
else {
$wrongGuess = 0;
$currentGuess = "";
}
echo "<form name = 'myForm' method='get' action='hangman.php'>";
# Alle geraden letters in een veriabele steken
if (isset($_GET['oldLetterGuessed'])) {
$oldLetterGuessed = $_GET ['oldLetterGuessed'];
$allLettersGuessed = $oldLetterGuessed . $currentGuess;
$allLettersGuessed;
echo "<input type = 'hidden' name = 'oldLetterGuessed' value ='". $allLettersGuessed ."'>";
echo $allLettersGuessed;
echo "<p> The number of wrong guesses so far is $wrongGuess </p>";
}
else {
echo "<input type = 'hidden' name = 'oldLetterGuessed' value = '' >";
}
echo "<input type = 'hidden' name = 'wrongGuess' value= '$wrongGuess'>";
if ($wrongGuess < 6) {
$alphas = range('A', 'Z');
# display letter buttons
for ($i=0; $i < count($alphas) ; $i++) {
echo "<input type = 'submit' name = 'letterGuessed' value = '" . $alphas[$i] . "' >";
}
}
else {
#The Screen when you're lost
echo nl2br("Oops looks like you've lost ");
echo nl2br("\n");
echo "The word you had to guess was ". $solution;
echo nl2br("\n");
echo "<form method = 'post' action = 'Restart()'>";
echo "<input type = 'submit' value = Restart'>";
echo "</form>";
}
echo "</form>";
#Display omzetten naar lijnen
$display = $solution;
$length = strlen($display);
for ($i=0; $i < $length ; $i++) {
$display[$i] = "-";
}
if (isset($oldLetterGuessed)) {
if ($oldLetterGuessed == "") {
$display = "_ _ _ _ _ _";
}
else
{
$currentGuess = $_GET['letterGuessed'];
# Looks at every letter and checks if it's the same
if (HasBeenUsed($solution, $currentGuess) == true ) {
# Positie zoeken $currentGuess in $solution
$posLetter = strpos($solution, $currentGuess);
# _ omzetten in de gegokte letter
if ($posLetter !== NULL) {
$display[$posLetter] = $currentGuess;
}
}
}
if ($display != $solution) {
echo $display;
}
else {
echo "You guessed the word";
}
}
?>
</body>
</html>
You can use sessions to store correct answers and their positions, here is an example based on your code:
Firstly start the session:
session_start();
Then you can store the correct guess and its position, this way:
$_SESSION['guessed'][$posLetter] = $currentGuess;
Above I create a session I called guessed to which I add the position and the current correct guessed letter.
Then when your page reloads it needs to display all correct guessed letters on their right position, so you need to loop on the session we created above :
if(isset($_SESSION['guessed'] )) { // Check if this session already exist
foreach ($_SESSION['guessed'] as $position=>$guessed ) {
$display[$position]=$guessed; // Assign the correct answer to the right position
}
}
Also, I recommend you to use post method in your form instead of get, and for this kind of games better you use JavaScript so it's faster and the page doesn't have to reload on each guessed letter.
Good luck!
every time you are guessing a number there is a server call, and the previous guessed letter no longer exists!
so, what you need to do is use $_SESSION :)
i will demonstrate it here:
in your PHP code:
<?php
session_start();
$_SESSION[word]=array();
// if a letter is correct just push it to your session array:
array_push($_SESSION[word],$letter);
// then you iterate the session array and according to the letters that resides there
// you revile the correct letters
In my database, the table table_log where I get the data from has 3 columns but I only need: (1) id (2) start (3) end. there are multiple logs with the same id and different start and end values. I'm trying to get the start values so I've placed said values in an array storage. Now, what I'm trying to do is place all values in the arrow in a single cell in the table but only the first value [0] appears not unless I make another column. How can I resolve this?
Query:
$logs = mysql_query("SELECT start FROM table_log WHERE id = '". $resultID."'");
$storage = Array();
while ($res = mysql_fetch_array($logs, MYSQL_ASSOC)) {
$storage[] = $res['start'];
}
Loop:
for($i = 0; $i < count($res); ++$i) {
if(count($res) > $i) {
$start_log = " $storage[$i] <br>";
}
}
$data .= "<td>". $start_log ."</td>";
Database:
id | start | end
01 | 10:00 | 11:00
01 | 12:00 | 01:00
01 | 03:00 | 03:30
Needed Output:
id | start | end
01 | 10:00 | 11:00
| 12:00 | 01:00
| 03:00 | 03:30
First of all - use same variable names ($storage & $storeArray & $res)
Secondly - mysql is removed in php7, instead try to make mysqli or PDO;
Do you want outputting only start times or all data???
$logs = mysql_query("SELECT start FROM table_log WHERE id = $resultID");
$storage = Array(); // from PHP5.6 use fetch_all
while ($res = mysql_fetch_assoc($logs)) {
$storage[] = $res['start'];
}
$output = "<div><b>$resultID:</b>". implode (' | ', $storage) ."</div>";
<?php
$logs = mysql_query("SELECT start FROM table_log WHERE id =$resultID");
while ($res = mysql_fetch_array($logs)) {
?>
<table>
<tr>
<th>id</th>
<th>start</th>
<th>end</th>
</tr>
<tr>
<td><?php echo $res['id'];</td>
<td><?php echo $res['start'];</td>
<td><?php echo $res['end'];</td>
</tr>
</table>
<?php
}
?>
got it! this one worked for me :)
$start_log = "";
$j = 1;
while ($res = mysql_fetch_array($logs, MYSQL_ASSOC)) {
$storage[] = $res['start'];
$j++;
}
for($i = 0; $i<=$ctr; $i++) {
$start_log .= " $storage[$i] <br>";
}
I have a task to create a script which will output multiplication table just for specified number. To create regular multiplication table, for example 10x10 we would write something like this:
echo "<table border=\"1\">";
for ($r =0; $r < $rows; $r++){
echo'<tr>';
for ($c = 0; $c < $cols; $c++)
echo '<td>' .$c*$r.'</td>';
echo '</tr>'; // close tr tag here
}
echo"</table>";
But output which i am supposed to receive, for example for digit "3", should look like this:
|1 x 1 = 1|1 x 2 = 2|1 x 3 = 3|
| ------- | ------- | ------- |
|2 x 1 = 2|2 x 2 = 4|2 x 3 = 6|
|3 x 1 = 3|3 x 2 = 6|3 x 3 = 9|
Anyone got an idea how to echo this using php (while and/or for) loops?
It sounds like you want to output the text of the calculation as well as the result, e.g. 1 x 3 = 3. You're missing that from your output.
Also, you need to start your for loops at 1 rather than 0, otherwise you get 0 x 0 = 0 which I assume you don't want. You would compensate for the loss of iterations by using <= instead of < in your condition of the for loop so you still get 3 iterations (in this example).
Try this:
echo '<table border="1">';
for ($r = 1; $r <= $rows; $r++) {
echo '<tr>';
for ($c = 1; $c <= $cols; $c++) {
echo sprintf('<td>%d x %d = %d</td>', $r, $c, $c * $r);
}
echo '</tr>'; // close tr tag here
}
echo '</table>';
Your expected output also seems to suggest that the first row should be a heading. It would seem odd to me to do that, but if that is the case, you'll need to do this instead:
$cellType = ($r === 1) ? 'th' : 'td'; // use <th> for the first row, otherwise <td>
echo sprintf('<%s>%d x %d = %d</%s>', $cellType, $r, $c, $c * $r, $cellType);
im in serach for a generic piece of code that uses and array
$arr[$key1][$key2] = $value;
output should be like this where "SUM" is not part of the array.
| 1st key2 | 2nd key2 | 3rd key2 | SUM
1st key1 | 10 | 10 | 10 | 30
2nd key1 | 10 | 10 | 10 | 30
3rd key1 | 10 | 10 | 10 | 30
SUM | 30 | 30 | 30 | 90
so i startet an output to see how far i get:
echo '<table width="100%"><thead>';
foreach($arr as $linekey => $line)
{
echo '<tr>';
echo '<th align="center">';
echo '</th>';
foreach($line as $key => $value)
{
echo '<th align="center">';
echo $key;
echo '</th>';
}
echo '<th align="center">';
echo 'SUM'; //adding the SUM column
echo '</th>';
echo '</tr>';
break;
}
echo '</thead><tbody>';
foreach($arr as $key1 => $value1)
{ echo '<tr>';
echo'<td>'.$key1.'</td>';
$sumRow = 0; //reset sumRow
foreach($arr[$key1] as $key2 => $value2)
{
echo'<td>'.round($arr[$key1][$key2],0).'</td>';
$sumRow += $arr[$key1][$key2]; //summing up rows
$sumAll += $arr[$key1][$key2]; //summing up everything
$sumCol += $arr[$key1][$key2]; //where should be this?
}
echo'<td>'.round($sumRow,0).'</td>'; //echo $sumRow
echo '</tr>';
}
echo '</tbody></table>';
this alaredy works but im not sure where to sum the columns
You should use an array $sumCol to gather columns sums:
$sumCol[$key2] += $arr[$key1][$key2];
It size should be as number of columns.
You cannot do it in one loop without an array because you loop over columns index internally, so you could gather only sumRow in one temporary variable (without array).
Then, at the end:
echo '<tr><td>SUM</td>';
foreach($sumCol as $key2 => $value2)
{
echo'<td>'.round($sumCol[$key2],0).'</td>'; //echo $sumCol
}
echo '</tr>';
echo '</tbody></table>';
The other way is to define the second loop where you iterate first over columns and at second, internally, over rows.