php foreach(database row as key => value)

HI

i am trying to get my code neater and have run in to  a problem i have a database quere that only brings back one row of info but i am looking for some form of for each commande that will condence the aftermathn i have attached an example of code not the stuff i ma useing but you will see what i maen any one know how i can get rid of the many lines of code below the query

in the real query i have about 40 columbs so am am just looking for some thing that will replace the

$valX = $row['valX'];

so many times but all the i can find is related to get many rows of 2 dits of data EG lists of names and ages not one row of 50 things any help would be great
$query = "select 
table1.val1,
table2.val2,
table2.val3,
table3.val4,
table4.val5,
from
table1
right join table2 on table1.val1 = table2.val1
left join table3 on table2.val2 = table3.val2
left join table4 on table2.val2 = table4.val2
where val1 = 'somthing'"
$get_query = mysql_query($query, $connector) or die(mysql_error());

$row_query = mysql_fetch_assoc($get_query);
$totalroes_get_query = mysql_num_rows($get_query);

$val1 = $row_get_query['val1'];
$val2 = $row_get_query['val2'];
$val3 = $row_get_query['val3'];
$val4 = $row_get_query['val4'];
$val5 = $row_get_query['val5'];

Open in new window

TestieMcTestAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Beverley PortlockCommented:
I would use an array to hold the results instead of $val1, $val2, etc so I would have $val[1], $val[2], etc and I would access then like so


    $val = array();
   
    for ( $i=1; $i < 41; $i++ ) {
        $columnName = "val$i";
        $val[$i] = $row_query [ $columnName ];
    }

If you simply must have them as separate variables then alter the code like so


   
    for ( $i=1; $i < 41; $i++ ) {
        $columnName = "val$i";
        $$columnName = $row_query [ $columnName ];
    }

    echo "$val1 $val2 $val3";

I have set the limit to 41 in the loop because you mentioned 40 and your variables start at 1 rather than 0. YOu may need to change that number.

0
TestieMcTestAuthor Commented:
Hi

i don't think i explaned my self right below is the real code for the search i have not go numbered values in anything

mysql_select_db($database_onecall, $onecall);
$query_mail_info = "SELECT
oc_customers.oc_cust_id,
oc_customers.m3_ref,
oc_customers.cust_title,
oc_customers.cust_firstname,
oc_customers.cust_lastname,
oc_customers.phone1,
oc_customers.email,
payment_to_quote_link.link_id,
payment_to_quote_link.card_id,
payment_to_quote_link.quote_id,
payment_to_quote_link.acc_id,
account_details.account_holder_name,
account_details.account_number,
account_details.sort_code,
car_quote.source,
car_quote.hear,
car_quote.veh_reg_no,
car_quote.quote_ref,
car_quote.veh_reg_date,
car_quote.volunt_excess,
car_quote.cover_date,
car_quote.cover_time,
car_quote.prem_val,
car_quote.org_prem_val,
car_quote.bd_type,
car_quote.bd_price,
car_quote.lp_type,
car_quote.lp_price,
car_quote.excess_protector,
car_quote.last_insurer,
car_quote.last_pol_num,
car_quote.comments,
car_quote.quote_date,
car_quote.locked,
car_quote.user_id,
car_quote.pid,
car_quote.comp_site,
carddetails.card_type,
carddetails.card_holder_name,
carddetails.card_number,
carddetails.issue_number,
carddetails.from_month,
carddetails.from_year,
carddetails.to_month,
carddetails.to_year,
carddetails.card_sec_code,
payments.payment_id,
payments.payment_type,
payments.cc_payment,
payments.dd_payment,
payments.dd_date,
payments.processed
FROM
oc_customers
Right Join payment_to_quote_link ON oc_customers.oc_cust_id = payment_to_quote_link.oc_cust_id
Left Join account_details ON payment_to_quote_link.acc_id = account_details.acc_id
Left Join car_quote ON payment_to_quote_link.quote_id = car_quote.carq_id
Left Join carddetails ON carddetails.card_id = payment_to_quote_link.card_id
Left Join payments ON payment_to_quote_link.link_id = payments.link_id
WHERE carq_id = '$carq_id'";
$mail_info = mysql_query($query_mail_info, $onecall) or die(mysql_error());
$row_mail_info = mysql_fetch_assoc($mail_info);
$totalRows_mail_info = mysql_num_rows($mail_info);

//foreach($row_mail_info as $key => $value);




$oc_cust_id = $row_mail_info['oc_cust_id'];
$m3_ref = $row_mail_info['m3_ref'];
$cust_title = $row_mail_info['cust_title'];
$cust_firstname = $row_mail_info['cust_firstname'];
$cust_lastname = $row_mail_info['cust_lastname'];
$phone1 = $row_mail_info['phone1'];
$email = $row_mail_info['email'];
$link_id = $row_mail_info['link_id'];
$card_id = $row_mail_info['card_id'];
$quote_id = $row_mail_info['quote_id'];
$acc_id = $row_mail_info['acc_id'];
$account_holder_name = $row_mail_info['account_holder_name'];
$account_number = $row_mail_info['account_number'];
$sort_code = $row_mail_info['sort_code'];
$source = $row_mail_info['source'];
$hear = $row_mail_info['hear'];
$veh_reg_no = $row_mail_info['veh_reg_no'];
$quote_ref = $row_mail_info['quote_ref'];
$veh_reg_date = $row_mail_info['veh_reg_date'];
$volunt_excess = $row_mail_info['volunt_excess'];
$cover_date =  $row_mail_info['cover_date'];
$cover_time = $row_mail_info['cover_time'];
$prem_val = $row_mail_info['prem_val'];
$org_prem_val = $row_mail_info['org_prem_val'];
$bd_type = $row_mail_info['bd_type'];
$bd_price = $row_mail_info['bd_price'];
$lp_type = $row_mail_info['lp_type'];
$lp_price = $row_mail_info['lp_price'];
$excess_protector = $row_mail_info['excess_protector'];
$last_insurer = $row_mail_info['last_insurer'];
$last_pol_num = $row_mail_info['last_pol_num'];
$comments = $row_mail_info['comments'];
$quote_date = $row_mail_info['quote_date'];
$locked = $row_mail_info['locked'];
$user_id = $row_mail_info['user_id'];
$pid = $row_mail_info['pid'];
$comp_site = $row_mail_info['comp_site'];
$card_type = $row_mail_info['card_type'];
$card_holder_name = $row_mail_info['card_holder_name'];
$card_number = $row_mail_info['card_number'];
$issue_number = $row_mail_info['issue_number'];
$from_month = $row_mail_info['from_month'];
$from_year = $row_mail_info['from_year'];
$to_month = $row_mail_info['to_month'];
$to_year = $row_mail_info['to_year'];
$card_sec_code = $row_mail_info['card_sec_code'];
$payment_id = $row_mail_info['payment_id'];
$payment_type = $row_mail_info['payment_type'];
$cc_payment = $row_mail_info['cc_payment'];
$dd_payment = $row_mail_info['dd_payment'];
$dd_date = $row_mail_info['dd_date'];
$processed = $row_mail_info['processed'];

Open in new window

0
Beverley PortlockCommented:
Ok.... try this untested code

foreach($row_mail_info as $key => $value) {

     $$key = $value;

     if ( $cust_title == "this is a test") {
          do something
     }
}

Remember that each iteration of the loop will overwrite the variables so they have to be used immediately.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Beverley PortlockCommented:
Oops.. not my day. Each iteration will not overwite the data.  We need TWO loops, one to process each row of data and within that one to generate the variable names

while ( $row_mail_info = mysql_fetch_assoc($mail_info) ) {

     foreach($row_mail_info as $key => $value) {
 
          $$key = $value;

          if ( $cust_title == "this is a test") {
               do something
          }
     }

     
}

Unless you only have one row to do.
0
TestieMcTestAuthor Commented:
this worked great thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.