0

I have stored the data in a mysql database, I want to know how I can split the data in each different array before output them in php?

<?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>";
    }
  }
?>

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>

I want to split them up by turn into array and output them with each different array to something like this:

mydata1

mydata2

mydata3

mydata4

How I can split the data into array before I could output them in my php?

4

4 Answers 4

1

Are you looking for something like this?

(consider it as an example, but, as @BenM, consider using PDO)

$sql = 'SELECT * FROM country';
$query = mysql_query($sql);
$countries = array();
while ($country = mysql_fetch_assoc($query)){ $countries[] = $country; }

This will allow you to use

foreach ($countries as $country) {
    echo $country['name'];
}
Sign up to request clarification or add additional context in comments.

4 Comments

@chandresh_cool: I want to split the data from mysql to turn into array before output them in <span id="text">. any idea how i can do this?
@chrisoojer Thats exactly what this does.
You could use the method I've described, and then, inside foreach you would do something like echo '<span id="text">'.$country['name'].'</span>'; according to the example. Is it what are you looking after?
@chrisoojer ..this is EXACTLY what my answer does, just cleaner and less code.
1

Your data is already in an array. That's what this line does:

 while ($row = mysql_fetch_array($result1))
{
  echo "<tr><td>".$row['mydata']."</td></tr>";
}

If you need to access the array outside of the while loop you could do this..

$myArray = array();
    while ($row = mysql_fetch_array($result1)){
    echo "<tr><td>".$row['mydata']."</td></tr>";
    foreach($row as $key=>$val){
        $myArray[$key] = $val;
    }
}

actually.. assuming your query returns multiple rows it would be a multidimensional array..

$myArray = array();
$i = 0;
    while ($row = mysql_fetch_array($result1)){
    echo "<tr><td>".$row['mydata']."</td></tr>";
    foreach($row as $key=>$val){
        $myArray[$i][$key] = $val;
        $i++;
    }
}

8 Comments

You don't understand what i am trying to achieve. I want to split the data before output them in php. The code you post isn't doing anything. i want my data to display on my php to something like this: my data 1
..then you're using it wrong. do it and then var_dump the array and you'll see all your data.
i don't want to display all of these data in my php. i want to use an array to choose what data i want to output in my php. here is an example: my data 1 my data 2 my data 3 my data 4. I want to use an array of 2 to display the data "my data 3", how i can do this?
This question is ambiguous as I do not know how many rows or the column names of your table. If you're using an associative array the table's column names are going to be the indexes of your array.. so I'm not sure if "my data 1" is what's in one of your rows or what...
thank you dreamCoder, i have nearly done it. there is a problem with my data that displaying on my php page: mydata1mydata1mydata1mydata1mydata1 here is the code i use: while ($row = mysql_fetch_array($result1)) { $myArr[] = $row['mydata']; echo $myArr[1]; } foreach($myArr as $key=>$value) { $myArr[$key] = str_replace(" ","",$value); }
|
1

If I am right than along with getting data in array you want to remove the space in between the data try this

while ($row = mysql_fetch_array($result1))
{
  echo "<tr><td>".$row['mydata']."</td></tr>";
  $myArr[] = $row['mydata']; 
}

foreach($myarr as $key=>$value)
{
   $myArr[$key] = str_replace(" ","",$value);
}

str_replace will remove the spaces.

Comments

0

You can used this,

 $db->setQuery($query);
$rows = $db->loadObjectList();


$i=0;
foreach($rows as $row){
        //$row; 
        $image = json_decode($row->images);
// split code
     $str     = utf8_encode($row->introtext);
     $order   = array("<p>", "</p>", "\n", "\r");
     $replace = '';
    $introtext = str_replace($order, $replace, $str);

    $introtext_split = explode("|", $introtext);

     $data[0]->designation  =  preg_replace('#<br />?#', "\n", $introtext_split[0]);

     $data[0]->description  =  preg_replace('#<br />?#', "\n\n", $introtext_split[1]);

     $data[0]->small_description  =  preg_replace('#<br />?#', "\n\n", $introtext_split[2]);
}

In data used any tag, Or this tag replace using following section :

$str     = utf8_encode($row->introtext);
 $order   = array("<p>", "</p>", "\n", "\r");

I think its useful to you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.