Jump to content

separating data after query?


acctman

Recommended Posts

I'm having trouble trying to separate the output into variables that I can use/echo on my page. when I do a print I see the two rows of data all grouped together how can I separate each result base on field and row? maybe something like $line['m_id'][0], $line['m_name'][0], $line['m_id'][1], $line['m_name'][1], etc...

 

 

$bio = mysql_query("SELECT * FROM soc_meminfo 
            WHERE m_id = '".mysql_real_escape_string($en['mm_id'])."'");
                             
if (mysql_num_rows($bio) == 0) call404();
while ($line = mysql_fetch_assoc($bio)) {
foreach ($line as $key => $value) {
        $en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value));
        }
echo '<pre>';
    print_r($line);
echo '</pre>';
}

Link to comment
https://forums.phpfreaks.com/topic/255678-separating-data-after-query/
Share on other sites

why wouldn't you simply access the db values via the assoc array that you already have available?

 

$field_name = $line['field_name'];

 

yes i tried that $line['m_id'] $line['m_pos'] etc... that works but it does not separate between rows. there is 1 -2 rows of data coming back from the table. so if i did $line['m_id'] i would get two of them and I won't be able to tell which is which.

Or just keep it in a numerical index based array and use a foreach loop later:

 

$bio = mysql_query("SELECT * FROM soc_meminfo 
            WHERE m_id = '".mysql_real_escape_string($en['mm_id'])."'");
                             
if (mysql_num_rows($bio) == 0) call404();

$lines = array();
while ($line = mysql_fetch_assoc($bio)) {
    $lines[] = $line;
}

// Then later on:
foreach ($lines as $line) {
     echo nl2br($line['field_name']) . PHP_EOL;
     echo nl2br($line['field_name2']) . PHP_EOL;
     // or inner loop as well:
     foreach ($line as $val) {
         echo nl2br($val) . PHP_EOL;
    }
}

 

Then it is one loop, and no mucking about with creating your own design.

 

Edit: added the nl2br, it does the same thing as your str_replace, but with the proper method for it.

this is confusing me a bit, I have an idea... is there a way to add an increment number to $line for every complete loop? so then i'll have $en['bm_pos0'] and $en['bm_pos1'] this will allow me to use <%bm_pos0%> and <%bm_pos1%> in my template coding. So how can I add just a number after each complete loop (row of fields)

 

while ($line = mysql_fetch_assoc($bio)) {   
foreach ($line as $key => $value) {
    $en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value));
    }
}

something like this i'm trying to do. I think my order is wrong for the increment

 

if (mysql_num_rows($bio) == 0) call404();
$i=1; 
while ($line = mysql_fetch_assoc($bio)) {   
$line . $i;
    foreach ($line as $key => $value) {
        $en['b'.$key] = str_replace("\n",'<br/>',stripslashes($value));
        }
        $i++;
    }
echo $en['bm_pos1'];

i figured it out...

 


$i = 1;
if (mysql_num_rows($bio) == 0) call404();
    while ($line = mysql_fetch_assoc($bio)) { 
        foreach ($line as $key => $value) {
        $en['b'.$key . $i] = str_replace("\n",'<br/>',stripslashes($value));
        }
    $i++;        
    }

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.