Jump to content

Recommended Posts

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 by Raz3rt

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.

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
     }
}

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.

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 by AbraCadaver

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 by Raz3rt
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.