SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Again begging regex help...:)

    Hi All,

    I have collection of HTML tables like following which i get from a database table field:
    HTML Code:
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 1</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 2</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 3</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 4</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 5</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    And i want to get each table separately in array or different variables. Can anyone help me how can i do this? I am really not being able though maybe it is very easy with simple regex or something else.

    Thank you in advance.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  2. #2
    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)
    Regex can do this, but you'd seriously be much better off using the DOM. I cannot give you an example because I haven't done much in the way of that kind of thing, but I'm certain that a few members here will be able to help you out with the practical side of things.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    SitePoint Member
    Join Date
    Feb 2009
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do something like this. You have to create a data / recordset for each loop/while/array

    Something like this;

    HTML Code:
    <?php require_once('dbfile-to-connect'); ?>
    <?php
    mysql_select_db($database_connection, $connection);
    $query_table1data = "SELECT * FROM tablewithdata1";
    $table1data = mysql_query($query_table1data, $connection) or die(mysql_error());
    $row_table1data = mysql_fetch_assoc($table1data);
    $totalRows_table1data = mysql_num_rows($table1data);
    
    mysql_select_db($database_connection, $connection);
    $query_table2data = "SELECT * FROM `tablewithdata2`";
    $table2data = mysql_query($query_table2data, $connection) or die(mysql_error());
    $row_table2data = mysql_fetch_assoc($table2data);
    $totalRows_table2data = mysql_num_rows($table2data);
    ?>
    
    <table width="100&#37;" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 1</td>
      </tr>
      <tr>
        <td><?php do { ?>
          <?php echo $row_table1data['DATA']; ?>
          <?php } while ($row_table1data = mysql_fetch_assoc($table1data)); ?> </td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 2</td>
      </tr>
      <tr>
        <td><?php do { ?>
          <?php echo $row_table2data['DATA']; ?>
          <?php } while ($row_table2data = mysql_fetch_assoc($table2data)); ?> </td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 3</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 4</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 5</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <?php
    mysql_free_result($table1data);
    
    mysql_free_result($table2data);
    ?>

  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Regex can do this, but you'd seriously be much better off using the DOM. I cannot give you an example because I haven't done much in the way of that kind of thing, but I'm certain that a few members here will be able to help you out with the practical side of things.
    Ahh.. yes i think i did it finally with DomDocument:
    Code php:
    $string = '<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 1</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 2</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 3</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 4</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td colspan="2">Table 5</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>';
     
    function innerHTML(DOMNode $oNode){
    	$oDOM = new DOMDocument();
    	foreach ($oNode->childNodes as $oChild){
    		$oDOM->appendChild($oDOM->importNode($oChild, true));
    	}
    	return $oDOM->saveHTML();
    }
    $tables = array();
    $oDOM = @DOMDocument::loadHTML($string);
    foreach ($oDOM->getElementsByTagName('table') as $oNode){
    	$bgcolor = ($bgcolor == 'f1f1f1') ? 'e0e0e0' : 'f1f1f1';
        $tables[] = '<table class="clsPrdInfo" style="background:#' . $bgcolor . ';">' . innerHTML($oNode) . '</table>';
    }
    echo '<pre>';print_r($tables);die();

    Thank you arkinstall for pointing...
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •