Choose where to echo HTML elements

I was wondering if it is possible to echo HTML elements on a page based on some condition. For example, let’s say that I have two tables on the page, and I want to insert some rows, one after another, in the first table or in the second one, based for example on the content of the rows. Is something like this possible using PHP only?

You can try using regular expressions to do searches on the actual HTML and then modify it correspondingly. Or you can try using DOMDocument to load the HTML by a parser and use those DOMDocument functions to search and modify the HTML correspondingly.

Try looking into those two options on php.net’s manual

Of course it’s possible :slight_smile: Though I think either me or svcghost have got the wrong end of the stick, I think you meant you want to insert data into two tables depending on certain information.

Essentially what you need to do is do all the processing before outputting the tables. Split data into two arrays, and THEN output the tables.

Pseudo:

<?php
$Data = array(1 => 'One', 2 => 'Two', 3 => 'Three', 4 => 'Four');
$Table1 = array();
$Table2 = array();
foreach($Data as $Key => $Value){
    if($Key % 2 != 0){
        $Table1[$Key] = $Value;
    }else{
        $Table2[$Key] = $Value;
    }
}
?>
<h2>Odd Numbers</h2>
<table>
    <tr>
        <th>Number</th>
        <th>Name</th>
    </tr>
    <?php
        foreach($Table1 as $Key => $Value){
            echo '<tr><td>', $Key, '</td><td>', $Value, '</td></tr>';
        }
    ?>
</table>
<h2>Even Numbers</h2>
<table> <!-- table 2 -->
    <tr>
        <th>Number</th>
        <th>Name</th>
    </tr>
    <?php
        foreach($Table2 as $Key => $Value){
            echo '<tr><td>', $Key, '</td><td>', $Value, '</td></tr>';
        }
    ?>
</table>

That’s exactly what I meant :wink: Thank you both for your help!

One more question: since my data is actually the result of a SQL query, I should probably use multidimensional arrays, right? if yes, I am probably doing something wrong here:


<?php
	
			$sql2 = "SELECT field1, field2, field3 FROM table";
			$result2 = mysqli_query($conn,$sql2);
			while ($row = mysqli_fetch_array($result2)) {
				if (condition is true) {
					$array_table1[]['field1'] = $row['field1'];
                                        $array_table1[]['field2'] = $row['field2'];
                                        $array_table1[]['field3'] = $row['field3'];
				}
			}
			echo '<table>
			<thead>
			<tr>
			<th>Column 1</th>
			<th>Column 2</th>
			<th>Column 3</th>
			</thead>
			</tr>
			<tbody>';

			        foreach($array_table1 as $value){
				    echo '<tr>';
			            foreach($array_table1[] as $value1) {
				        echo '<td>', $value1, '</td>';
				    }
				    echo '</tr>';
			        }
	
			
			echo '</tbody>';
			echo '</table>';
		
		?>

I imagine that the problem is in the way I cycle through the array :stuck_out_tongue:

Edit: sorry for the bad formatting, but this is just an extract of a larger piece of code :stuck_out_tongue:

An array is just an array of items - a multidimensional array is simply a special case where those items are arrays. That’s all there is to it :stuck_out_tongue:

What you have got wrong is the addition of an item to an array. By using $array_table1[‘Something’] you’re creating a NEW item in the $array_table1 array and trying to treat that new item as an array (which it isn’t, as it hasn’t been defined). “$array” means ‘add an item to $array’, essentially.

To make everything easier, what you’re trying to achieve can be done in a single line:

while ($row = mysqli_fetch_array($result2)) {
	if (condition is true) {
		$array_table1[] = $row;
	}
}

I didn’t know that I could do this so easily :stuck_out_tongue:

I though that by writing

$array_table1[]['field3'] = $row['field3'];

I could add the value of the field “field3” in the array “row” to the field “field3” in “array_table1”. Just out of curiosity, isn’t it possible to do something like this? Meaning, addressing a specific cell in a multidimensional array like this?

I could add the value of the field “field3” in the array “row” to the field “field3” in “array_table1”. Just out of curiosity, isn’t it possible to do something like this? Meaning, addressing a specific cell in a multidimensional array like this?[/QUOTE]

Yes, but arrays are not homogenous.
If… lets say your array_table1 has 3 elements in it atm. (0,1,2).
Each of those elements can be ANYTHING. Image resources, arrays, strings, etc. There is no dependancy or formatting to an array object. Just because the thing at index 0 is a 3-item array, doesnt have any bearing on what might be stored at index 1 or 2.

If I do…
$array_table1[‘field3’] = $row[‘field3’];

Now there’s a fourth element; That fourth element is an array, of size 1. It’s 1 element is associatively named field3, and contains what $row[‘field3’] had in it.

If I then do
$array_table1[‘field2’] = $row[‘field2’];

I added ANOTHER element to array_table1. There’s now five of them. That new element is, as the fourth was, an array of size 1.

Could you do it by something like…


while($row = mysql_fetch_array($someresult) {
  $next = count($array_table1); //Count is always the size of the array; because numerical array indexing starts at 0, we can use this value as 'the next element to be filled.'
  $array_table1[$next]['field1'] = $row['field1'];
  $array_table1[$next]['field2'] = $row['field2'];
  ....
}

sure. It’s inefficient, but you could do it that way.

Understood, thank you :wink:

Edit: I’ve just realized that i made a mistake in what I meant. I meant to write something like this:


$array_table1[last+1]['field3'] = $row['field3'];

Where “last” would be the very last position in the array. At this point I wanted to cycle through all values contained in $row and enter them in $array_table1[last+1][i], with i going from 1 to the length of $row. When all the elements have been controlled I would increment last+1 to last+2.

I don’t know if you can understand what I mean :stuck_out_tongue: Anyways, that’s how I was thinking about doing it. I imagine that this can be inefficient too, but what I wrote in my last post should be even more inefficient right? :smiley:

“Last + 1” = $next in the example i had above. You were essentially trying to do what I put there.

It’s much more efficient just to throw the row into the array (being that the row is already a formatted array!), as Jake said.

I believe the way I put it above would be the most efficient, because its a single memory-copying process instead of multiple.

If you were to do it the way you’ve shown in your example, you would assign all of those values to a new array, e.g.:

$Arr = array();
$Arr['something'] = $row['something'];
$Arr['anotherthing'] = $row['anotherthing'];
$array_table1[] = $Arr;

Though that is a little bit wasteful.

What I wrote is a bit different (at least I think so :smiley: ). If you wrote


$Arr = array();
$Arr['something'][1] = $row['something'];
$Arr['something'][2] = $row['anotherthing'];
$array_table1[] = $Arr;

you wouldn’t create a new array for every new elements that you insert, right? You would just add elements to the array in position “something”.

That’s what I was trying to express with my (absolutely-needs-improvement) English xD

Anytime you use (with nothing inside), PHP translates it as “Tack this thing onto the end of the array, using whatever numeric key comes next.”

$Arr = array(); //Arr is an empty array.
$Arr['something'][1] = "Something here"; //$Arr now has an object at associative key 'something'. That object is an array. That array has an object at numeric key 1. That object is a string.
$Arr['something'][2] = "Something Else here"; //The array $Arr['something'] has an object at numeric key 2. That object is a string.

$array_table1[] = $Arr; // If Array_table1 didnt exist before, it exists now. It is an array. If it doesnt have anything in it at the moment, it gets an object at numeric key 0. That object is the array that we created before.
//So to reference the second string in array_table1, you would now need:
echo $array_table1[0]['something'][2];

That’s exactly what I meant! :smiley:

And I’ve just realized, by rereading your posts, that I make a mistake while reading that made me misunderstand what you meant :smiley: