SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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?

  2. #2
    SitePoint Addict svcghost's Avatar
    Join Date
    Oct 2010
    Posts
    288
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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

  3. #3
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Of course it's possible 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 Code:
    <?php
    $Data 
    = array(=> 'One'=> 'Two'=> 'Three'=> 'Four');
    $Table1 = array();
    $Table2 = array();
    foreach(
    $Data as $Key => $Value){
        if(
    $Key != 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>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  4. #4
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jake Arkinstall View Post
    Of course it's possible 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 Code:
    <?php
    $Data 
    = array(=> 'One'=> 'Two'=> 'Three'=> 'Four');
    $Table1 = array();
    $Table2 = array();
    foreach(
    $Data as $Key => $Value){
        if(
    $Key != 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 Thank you both for your help!

  5. #5
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    Code:
    <?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 :P

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

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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 :P

    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:

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

    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jake Arkinstall View Post
    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 :P

    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:

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

    I didn't know that I could do this so easily :P

    I though that by writing

    Code:
    $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?

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    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....
    PHP Code:
    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.

  9. #9
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Understood, thank you

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

    Code:
    $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 :P 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?

  10. #10
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by D3V4 View Post
    Understood, thank you

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

    Code:
    $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 :P 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?
    "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.

  11. #11
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.:
    PHP Code:
    $Arr = array();
    $Arr['something'] = $row['something'];
    $Arr['anotherthing'] = $row['anotherthing'];
    $array_table1[] = $Arr
    Though that is a little bit wasteful.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  12. #12
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jake Arkinstall View Post
    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.:
    PHP Code:
    $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 ). If you wrote

    Code:
    $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

  13. #13
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    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."

    PHP Code:
    $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]; 

  14. #14
    SitePoint Addict D3V4's Avatar
    Join Date
    May 2010
    Posts
    370
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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."

    PHP Code:
    $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!

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


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •