2

Hey guys I'm looking for a solution to a little problem, the PHP seems to be functioning properly and I can't see any problems with the SQL Query, can anybody see anything blatantly wrong with my code? I'm really stuck here.

the contents of the "items" row are similar to the following:

30->0,31->0,32->0,36->0,33->10,29->0,35->0,6->0,5->0,8->0,9->0,7->0,14->0,15->0,10->0,17->0

I just need them to explode into an array and foreach value in that array, print on the page with their appropriate "key". I've looked here: explode() into $key=>$value pair and arrived at no conclusion - this is the full code, if anybody could help me out, tell me what I'm doing or maybe even point me in the right direction I'd be really happy.

$id = $_GET['order'];
if(!is_number($id)){
    exit();
}
$sql = "SELECT * FROM ORDERS WHERE id='$id'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
    $dataString = $row["items"];
    foreach (explode(",", $dataString) as $cLine) {
    list ($cKey, $cValue) = explode('->', $cLine, 2);
        $itemarray[$cKey] = $cValue;
    }

    foreach($itemarray as $key => $value) {
        echo "<br/>".$value." x ".$key;
    }
    }
} else {
    echo "0 results";
}

Thanks.

5
  • It's probably something really obvious but I have intense brain-failure at the minute and have a deadline. Commented Sep 2, 2015 at 15:07
  • What are the data types of your fields in your orders table? (varchar, int, etc) Commented Sep 2, 2015 at 15:23
  • The string I'm trying to pull is a varchar, Steve. Commented Sep 2, 2015 at 15:24
  • Ok, what about the 'id' field? Commented Sep 2, 2015 at 15:25
  • Auto-incrementing INT field. Commented Sep 2, 2015 at 15:25

3 Answers 3

1

I can't comment yet, so I'm posting an answer. The following code seems to work fine:

$dataString = "30->0,31->0,32->0,36->0,33->10,29->0,35->0,6->0,5->0,8->0,9->0,7->0,14->0,15->0,10->0,17->0";
foreach (explode(",", $dataString) as $cLine) {
  list ($cKey, $cValue) = explode('->', $cLine, 2);
  $itemarray[$cKey] = $cValue;

  foreach($itemarray as $key => $value) {
    echo "<br/>".$value." x ".$key;
  }
}

so my best bet would be that the data returned from the query isn't as expected.

Sign up to request clarification or add additional context in comments.

1 Comment

Definitely a Query error then, you think? Thanks for running that, hadn't thought of doing so. I guess I'll have to tinker with the query until I find one that works. Thanks a bunch, dude.
1

For when you get the data worked out this should be easier:

parse_str(str_replace(array('->',','), array('=','&'), $row['items'), $itemarray);
print_r($itemarray);

3 Comments

Thank you very much, do you think I should re-format my database entires to use = and & as opposed to -> and ,?
If you have control over this I would restructure so each id and value is in their own row.
Trouble is, this is for an order system, the "key" in this function is the product and the value is the amount. All processed as one order. I guess I could assign some sort of unique identifier to each row. However this would conflict with the rest of the website. Thanks again.
0

Turns out my code and query are both perfect. The problem was here:

if(!is_number($id)){
    exit();
}

Either my ?order=55 wasn't recognized as a number or was misusing the is_number function.

Thanks for all your help guys.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.