my brain is stuck in a php loop…

For those of you who do not know, I also do a bit of programming. Started at my father’s web development firm, continued through my physics studies, and just kept doing it. I cannot describe why I keep learning more and doing it, other than to say that it fits the way my brain operates for a significant portion of most days. And I save butt-loads of money on my company website; seriously, good websites are not cheap. I am rambling. I wrote this post because I am having a problem with a php loop and it is pissing me off. They are so fucking simple. I just know that I am missing something obvious. But I really need to focus on an image algorithm I am working on and stop looking at this code.

So I am pasting the code here in the hopes that my programming friends can help me out. If not, then I will tackle it again next week.

$b_price = '51.30';//based on 1ct. AA Round db.yalismagems tb.variants f.B_PRICE
$row_number = '';

for ($row = 0; $row <=9; $row++ )
$cw_f = $m_cw[$row_number]['Mod Factor'].'';
//echo '|'.$m_cw[$row]['Weight'].'|'.$m_cw[$row]['Mod Factor'].'|';
$cw2_f = $m_cw[$row]['Weight'].$m_cw[$row]['Mod Factor'];
// echo $cw2_f;
//echo $m_cw[$row]['Weight'].'';

for ($row = 0; $row <=9; $row++ )
$weight = array($m_cw[$row]['Weight'] => $m_cw[$row]['Mod Factor']);

//$weight = array($m_cw[0]['Weight'] => $m_cw[0]['Mod Factor'],$m_cw[1]['Weight'] => $m_cw[1]['Mod Factor'],
// $m_cw[2]['Weight'] => $m_cw[2]['Mod Factor'] ,$m_cw[3]['Weight'] => $m_cw[3]['Mod Factor']);

$grade = array($m_gr[0]['Grade'] => $m_gr[0]['Mod Factor'],$m_gr[1]['Grade'] => $m_gr[1]['Mod Factor'],
$m_gr[2]['Grade'] => $m_gr[2]['Mod Factor'] ,$m_gr[3]['Grade'] => $m_gr[3]['Mod Factor']);

$opt1 = "Choose Weight";
$opt2 = "Choose Grade";

foreach (array_keys($weight) as $ptype) {
$c1 = ($ptype == $_GET[f1]) ? " SELECTED" : "";
$opt1.= "$ptype";

foreach (array_keys($grade) as $ptype) {
$c2 = ($ptype == $_GET[f2]) ? " SELECTED" : "";
$opt2.= "$ptype";

if ($_GET[f1] != "never") {
$result1 = $b_price*$weight[$_GET[f1]];
$state = sprintf("You Chose %s" ,
$_GET[f1], $result1);
$result = "First result - $state";
} else{
$result = "Incomplete data";
$result1 = 0;

if ($_GET[f2] != "never") {
$result2 = $result1*$grade[$_GET[f2]];
$state = sprintf("You Chose %s the new total is ". $result2 . "",
$_GET[f2], $result1);
$result .= "Second result - $state";
} else{
$result .= "Incomplete data";
if ($result1 == 0) $result = "Results";

The form elements [both select drop-downs] are called on the same page. I commented out the echo statements because they are used for testing. The first code statement in red is the one giving me a headache, the second one works. I just highlighted it because it is related to the process. The loop intended to populate the $weight variable with data is only returning the last index of my original three-dimensional array $m_cw (which is in the include file referenced on the first line). I set this array up with the following structure:

$m_cw = array( array( ‘Weight’ => ‘.50’,
‘Mod Factor’ => ‘.596’),
… more data…);

Everything works fine, so I know that this is probably something horribly simple. What the hell am I missing?


~ by aikaterine on July 24, 2007.

12 Responses to “my brain is stuck in a php loop…”

  1. Sorry, I never bothered learning much php beyond what I needed to get other peoples scripts to work. I just paid other people to do that stuff! I was always more of a CSS man myself..

  2. I don’t do PHP (it makes me shudder), so forgive me if I’m talking rubbish here as I don’t really understand the… uh… finer points of the language.

    But it looks to me like in each pass through the loop you’re assigning a new array to the $weight variable, Presumably, what you want to do involves pushing a value onto the end of the $weight array each time through the loop. So perhaps the following is what you want:

    $weight = [];
    for ($row = 0; $row <=9; $row++ ) {
    $weight[$m_cw[$row][‘Weight’]] = $m_cw[$row][‘Mod Factor’];

    I have no idea if this will actually work since I live in a blessed state of ignorance about PHP, but hopefully I’ve been helpful rather than just flaunting my ignorance…

  3. Hm. I seem to have omitted a bracket from the code above. Then again I’m not sure that it’s even legal PHP syntax with the bracket included…

  4. thank you guys for looking at it.

    darkentries – css can be fun, but I like the logic in languages like php. I had to learn c++, IDL, and a zillion other languages in my physics undergrad. There is something calming about formal logic to me.

    Experimental Chimp –

    You are right that it is assigning a new array on each pass through; and, for the life of me I cannot figure out the syntax to get it to stop. Your idea is on-target, but the wrong syntax for what I am trying to do. I am actually pulling data out of a three-dimensional array and creating a two-dimensional array $weight. So I need two elements on the other side of the equal sign. I know that the fix is simple, probably just some little syntax thing.

    The code I commented out underneath it – starting with “$weight=” works, it is the structure that the loop should ultimately return. The for loop is meant to replace that code.

    Again, you guys are great for taking a look at it.

  5. So you’re trying to create an array of arrays? This is my last guess, since I’m way out of my depth, but would something like this work?

    $weight = array();
    for ($row = 0; $row $m_cw[$row][‘Mod Factor’]));

  6. hm, your syntax is all wrong, but your logic might be on to something. Pretty good for a non-php person.

    I am going to think about it when my brain is better; after I get my sleeping back on track. Which will not be until tonight. God I want to sleep, but I know if I do things will only get worse.

  7. Bad WordPress, mangling my comment. No cookies for you. What I meant to suggest before WordPress decided that part of my comment was a really weird HTML tag was rewriting the inner loop as:

    array_push($weight, array($m_cw[$row][‘Weight’] => $m_cw[$row][‘Mod Factor’]));

  8. oooooh, that was good. Don’t know if it will work. But, again, the logic looks good. Better than I was coming up with, again I am at blank stage right now.

    hm, I will try that when I come back to this code tomorrow morning.

  9. fuck me I am repeating words in my comments like a two year old.

  10. Oh my goodness…physics and programming?

    You are awesome. I suck.

    Sorry, this in no way helps you with your php.

  11. You do not in any way suck. Your writing is indescribably better than mine. And you pick the most awesome anime pics to use in your little picture thingy. You know more about medical stuff than I do. I bet you are much nicer than I am. And you used the word ‘keen’ in a sentence. I could go on and on.

    You are awesome! And I hope you get in on…what did darkentries call it…madblogcon. It’s hard to say, but the more I read it the more I like it.

  12. Thanks, dear. I do like self-deprecating humour but one thing that I absolutely love is people that can do things that I can’t! It’s like…oh, I so admire you! It really was a compliment to you.

    Thanks for the compliment about the writing but yours is great too! Okay, we have a mutual admiration thing going on. And who says you’re not nice? I think you’re nice. I don’t know you very well but I like you already.

    Alright, I missed something. What the hell is madblogcon?
    I need to either skip over to his pad or you can just tell me.

    And here’s something that will make you laugh. sometimes I misread things–like when I read. Well, with people too but that’s another issue. Not quite a dyslexic thing but probably due to meds making my brain fried. When I read “…I hope you get in on…” I thought it said “…I hope you get it on…”


    Now I’ve got to skip on up to the orgasm post…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: