Raz3rt Posted May 21, 2014 Share Posted May 21, 2014 (edited) Hi, In my code i am fetching row per row out of my database through ->fetch(PDO::FETCH_ASSOC) this fetch have i put in a WHILE loop to fetch all the rows, one at a time while($select_rma_row = $select_rma_detail->fetch(PDO::FETCH_ASSOC)){ In my array $select_rma_row there are a lot of value's that are (int) 1 and i really want to echo them as "YES" (1 = YES) What is the best way to change every (int) 1 to "YES" in that array? also is there a whay i can call every $key inside that array? Like you do with a foreach i.e. : if $select_rma_row[$key] == 1 { $select_rma_row[$key] == "YES"; } Thank you! Edited May 21, 2014 by Raz3rt Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/ Share on other sites More sharing options...
sKunKbad Posted May 21, 2014 Share Posted May 21, 2014 (edited) $result = $stat->fetchAll(); foreach( $result as $k => $v ) { if( $v['whatever'] === 1 ){ $v['whatever'] = 'yes'; } // $k is your key, so do something with it } Edited May 21, 2014 by sKunKbad Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/#findComment-1480313 Share on other sites More sharing options...
Jacques1 Posted May 21, 2014 Share Posted May 21, 2014 Hi, I doesn't make sense to hard-code a “YES” or “NO” into your array, because like you said, this is only for displaying. Leave the array as it is and do the “YES/NO” thing when you actually display the content. By the way, you neither need fetchAll() nor the old while (... fetch()) pattern. You can use a PDOStatement directly in a foreach loop: <?php $rma_details_stmt = $database->query(' ... '); foreach ($rma_details_stmt as $rma_details) { // ... } You may have to adjust the fetch mode. This can be done per statement or globally for the whole database connection. Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/#findComment-1480325 Share on other sites More sharing options...
sKunKbad Posted May 21, 2014 Share Posted May 21, 2014 Hi, I doesn't make sense to hard-code a “YES” or “NO” into your array, because like you said, this is only for displaying. Leave the array as it is and do the “YES/NO” thing when you actually display the content. By the way, you neither need fetchAll() nor the old while (... fetch()) pattern. You can use a PDOStatement directly in a foreach loop: <?php $rma_details_stmt = $database->query(' ... '); foreach ($rma_details_stmt as $rma_details) { // ... } You may have to adjust the fetch mode. This can be done per statement or globally for the whole database connection. I think he may need nested foreaches, because he wants the keys (field names). foreach ($rma_details_stmt as $rma_details) { foreach( $rma_details as $k => $v ) { // $k is the key and $v is the value of the field } } Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/#findComment-1480335 Share on other sites More sharing options...
Jacques1 Posted May 21, 2014 Share Posted May 21, 2014 Well, like I said, I wouldn't replace the values in the first place. How the data is displayed is a matter of presentation, so it should be done in the HTML part (or whatever the data is used for). Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/#findComment-1480336 Share on other sites More sharing options...
Raz3rt Posted May 21, 2014 Author Share Posted May 21, 2014 Hi, I doesn't make sense to hard-code a “YES” or “NO” into your array, because like you said, this is only for displaying. Leave the array as it is and do the “YES/NO” thing when you actually display the content. By the way, you neither need fetchAll() nor the old while (... fetch()) pattern. You can use a PDOStatement directly in a foreach loop: <?php $rma_details_stmt = $database->query(' ... '); foreach ($rma_details_stmt as $rma_details) { // ... } You may have to adjust the fetch mode. This can be done per statement or globally for the whole database connection. Whow, i dont know why i didn't know that before! The only thing i see now is that the foreach does the same as a fethall() so i have every value 2 times in the array ... How can i solve this? Sorry for all the questions but i am still learning. Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/#findComment-1480353 Share on other sites More sharing options...
Jacques1 Posted May 21, 2014 Share Posted May 21, 2014 Not sure what you mean. You use the loop instead of fetchAll(). Are you still trying to replace the values? Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/#findComment-1480355 Share on other sites More sharing options...
AbraCadaver Posted May 21, 2014 Share Posted May 21, 2014 (edited) For the original question, if you don't need the 1 or 0 for some other reason (or even if you do, just include both the column and the column case), do it in the query: SELECT column_name, CASE WHEN column_name IS 1 THEN 'Yes' ELSE 'No' END AS something FROM table_name Or: SELECT column_name, IF(column_name=1,'Yes','No') AS something FROM table_name For the fetch issue: $select_rma_detail->setFetchMode(PDO::FETCH_ASSOC); Edited May 21, 2014 by AbraCadaver Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/#findComment-1480356 Share on other sites More sharing options...
Raz3rt Posted May 22, 2014 Author Share Posted May 22, 2014 (edited) Not sure what you mean. You use the loop instead of fetchAll(). Are you still trying to replace the values? I'm sorry i didn't want to be unclear! Well maybe it is better to show my whole code so you got a better idea of what i would like to have? Anyway here you have the code: 1. The select: $selectrmadetail = $dbh->prepare ("SELECT rd_rma_nr, rd_artikel_code, rd_serienr, rd_ontvangenklant, rd_onder_waarborg, rd_omr_rep, rd_teruggestuurdklant, rd_afgehandelklant FROM rma_detail LEFT JOIN rma ON rma_detail.rd_rma_id=rma.r_id LEFT JOIN klanten ON rma.r_klantid=klanten.k_id WHERE k_id = ?"); $selectrmadetail->bindParam(1, $_SESSION['k_id']); $selectrmadetail->execute(); 2. Publish it <?php foreach ($selectrmadetail as $selectrmadetailrow){ foreach ($selectrmadetailrow as $k => $v){ //var_dump($selectrmadetailrow); echo "<td>$v</td>" } } ?> So now when i execute this i got 3 problems 1. I want to have all of the rows nicely on the screen (row per row) 2. Now i have all of the value's 2 times on my screen. When i do var_dump i got this: array(16) { ["rd_rma_nr"]=> string(9) "201405022" [0]=> string(9) "201405022" ["rd_artikel_code"]=> string(7) "34-CXN3" [1]=> string(7) "34-CXN3" ["rd_serienr"]=> string(13) "5411284116133" [2]=> string(13) "5411284116133" ["rd_ontvangenklant"]=> string(1) "1" [3]=> string(1) "1" ["rd_onder_waarborg"]=> NULL [4]=> NULL ["rd_omr_rep"]=> NULL [5]=> NULL ["rd_teruggestuurdklant"]=> NULL [6]=> NULL ["rd_afgehandelklant"]=> NULL [7]=> NULL } array(16) { ["rd_rma_nr"]=> string(9) "201405022" [0]=> string(9) "201405022" ["rd_artikel_code"]=> string(7) "34-CXN3" [1]=> string(7) "34-CXN3" ["rd_serienr"]=> string(13) "5411284116133" [2]=> string(13) "5411284116133" ["rd_ontvangenklant"]=> string(1) "1" [3]=> string(1) "1" ["rd_onder_waarborg"]=> NULL [4]=> NULL ["rd_omr_rep"]=> NULL [5]=> NULL ["rd_teruggestuurdklant"]=> NULL [6]=> NULL ["rd_afgehandelklant"]=> NULL [7]=> NULL } 3. I want the rd_rma_nr to have a link wrapped arround it so when i click on it i can navigate to a detail page where in the header the rd_rma_number is send with (index.php?page=rma-detail&id=201405022). All that is possible with a WHILE loop exept in a while loop i didn't knew what to do to change the value 1 to "YES". Now you where saying i could better use a foreach so my first question isn't really an issue anymore. But now i could use some help because this will be the first time i am doing a fetch with a foreach. Edited May 22, 2014 by Raz3rt Quote Link to comment https://forums.phpfreaks.com/topic/288655-swap-value-of-fetchpdofetch_assoc-in-while-loop/#findComment-1480406 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.