acctman Posted January 24, 2012 Share Posted January 24, 2012 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/255678-separating-data-after-query/ Share on other sites More sharing options...
AyKay47 Posted January 24, 2012 Share Posted January 24, 2012 why wouldn't you simply access the db values via the assoc array that you already have available? $field_name = $line['field_name']; Quote Link to comment https://forums.phpfreaks.com/topic/255678-separating-data-after-query/#findComment-1310642 Share on other sites More sharing options...
acctman Posted January 24, 2012 Author Share Posted January 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255678-separating-data-after-query/#findComment-1310647 Share on other sites More sharing options...
Pikachu2000 Posted January 24, 2012 Share Posted January 24, 2012 Then echo a line break between each record. Maybe even increment a counter and echo it too. Quote Link to comment https://forums.phpfreaks.com/topic/255678-separating-data-after-query/#findComment-1310669 Share on other sites More sharing options...
premiso Posted January 24, 2012 Share Posted January 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/255678-separating-data-after-query/#findComment-1310670 Share on other sites More sharing options...
acctman Posted January 24, 2012 Author Share Posted January 24, 2012 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)); } } Quote Link to comment https://forums.phpfreaks.com/topic/255678-separating-data-after-query/#findComment-1310690 Share on other sites More sharing options...
acctman Posted January 24, 2012 Author Share Posted January 24, 2012 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']; Quote Link to comment https://forums.phpfreaks.com/topic/255678-separating-data-after-query/#findComment-1310696 Share on other sites More sharing options...
acctman Posted January 24, 2012 Author Share Posted January 24, 2012 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++; } Quote Link to comment https://forums.phpfreaks.com/topic/255678-separating-data-after-query/#findComment-1310729 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.