how to output the data into each tags?

Discussion in 'PHP' started by mark103, Apr 10, 2013.

  1. #1
    Hi guys,

    I have stored data in a MySQL database and I've output it in PHP. I want to know how I can output the data in different <span id="text1">` tags from `<tr><td>` tags.

    
        <?php
          define('DB_HOST', 'localhost');
          define('DB_USER', 'myusername');
          define('DB_PASSWORD', 'mypassword');
          define('DB_DATABASE', 'mydbname');   
       
          $errmsg_arr = array();
          $errflag = false;
          $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
       
           if(!$link)
          {
            die('Failed to connect to server: ' . mysql_error());
          }
          $db = mysql_select_db(DB_DATABASE);
       
          if(!$db)
          {
            die("Unable to select database");
          }
       
       
          if($errflag)
          {
            $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
            echo implode('<br />',$errmsg_arr);
          }
          else
          {
            $qrytable1="SELECT id, mydata FROM mydb ";
            $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
       
             while ($row = mysql_fetch_array($result1))
            {
              echo "<tr><td>".$row['mydata']."</td></tr>";
            }
          }
        ?>
    
    PHP:
    On my PHP page it show something like this:

    
        <tr><td>my data 1</td></tr><tr><td>my data 2</td></tr><tr><td>my data 3</td></tr><tr><td>my data 4</td></tr>
    PHP:
    How can I split the output the data into an array before I could output them in different <span id="text1">` tags from `<tr><td>` tags?

    any advice would be much appreicated.

    thanks in advance
     
    mark103, Apr 10, 2013 IP
  2. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #2
    I think this is what you are looking for:

    
     
    <?php
          define('DB_HOST', 'localhost');
          define('DB_USER', 'myusername');
          define('DB_PASSWORD', 'mypassword');
          define('DB_DATABASE', 'mydbname');   
       
          $errmsg_arr = array();
          $errflag = false;
          $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
       
           if(!$link)
          {
            die('Failed to connect to server: ' . mysql_error());
          }
          $db = mysql_select_db(DB_DATABASE);
       
          if(!$db)
          {
            die("Unable to select database");
          }
       
       
          if($errflag)
          {
            $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
            echo implode('<br />',$errmsg_arr);
          }
          else
          {
            $qrytable1="SELECT id, mydata FROM mydb ";
            $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
           
               $i =0;
             while ($row = mysql_fetch_array($result1))
            {
               echo '<span id="text'.$i.'">'.$row['mydata'].'</span>';    
               $i++;
              //echo "<tr><td>".$row['mydata']."</td></tr>";
            }
          }
        ?>
    
    PHP:

    On a side not you shouldn't really be using MYSQL functions it's outdated and insecure. You need to use MySQLi: http://php.net/manual/en/book.mysqli.php
     
    HuggyStudios, Apr 11, 2013 IP
  3. mark103

    mark103 Active Member

    Messages:
    110
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    53
    #3
    @HuggyStudios: Thanks for your post. I have copied and pasted the code in my php, but there is something not right there. It have output the data altogether without put into array.

    The data is displaying on my php:
    mydata1mydata2mydata3mydata4
    PHP:
    I want to split them up by turn into array and output them with each different array to something like this:
    mydata1
    PHP:
    mydata2
    PHP:
    mydata3
    PHP:
    mydata4
    PHP:
    Please post the fixed code instead of wasting my own time.
     
    mark103, Apr 11, 2013 IP
  4. HuggyStudios

    HuggyStudios Well-Known Member

    Messages:
    724
    Likes Received:
    20
    Best Answers:
    26
    Trophy Points:
    165
    #4
    There's no need to ask me to stop wasting your time, the best thing you could of done is used the php.net website to lookup the functions you need.

    http://php.net/manual/en/function.array-push.php

    I have completed the code for you, please read the comments within the code.
    
     
    <?php
          define('DB_HOST', 'localhost');
          define('DB_USER', 'root');
          define('DB_PASSWORD', '');
          define('DB_DATABASE', 'testing');   
       
          $errmsg_arr = array();
          $errflag = false;
          $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
       
           if(!$link)
          {
            die('Failed to connect to server: ' . mysql_error());
          }
          $db = mysql_select_db(DB_DATABASE);
       
          if(!$db)
          {
            die("Unable to select database");
          }
       
       
          if($errflag)
          {
            $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
            echo implode('<br />',$errmsg_arr);
          }
          else
          {
            $qrytable1="SELECT id, mydata FROM mydb ";
            $result1=mysql_query($qrytable1) or die('Error:<br />' . $qry . '<br />' . mysql_error());
           
               $i =0;
              // create array
              $stack = array();
             while ($row = mysql_fetch_array($result1))
            {
               // add to stack array
               array_push($stack, '<span id="text'.$i.'">'.$row['mydata'].'</span>');    
               $i++;
              //echo "<tr><td>".$row['mydata']."</td></tr>";
            }
           var_dump($stack);
           // output the array
           foreach($stack as $value) {
               echo $value.'<br />';    
           }
          }
        ?>
    
    PHP:
    I have tested this code and this is the output:
    [​IMG]
     
    HuggyStudios, Apr 12, 2013 IP
  5. annaharris

    annaharris Active Member

    Messages:
    119
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    51
    #5
    Thanks for providing the code as this is the exact output that I was looking for.
     
    annaharris, Apr 12, 2013 IP