-1

I am having issue data Array overwritten in foreach loop. Result I am getting like this wrongRight together .Right answer is showing but also wrong for example ZucchiniCauliflower.Please help

Image

enter image description here

CODE 1

$data = array();
$dis_07= null;
$dis_03 = null;

if (is_array($row)) {
    foreach ($row as $value) {
        $gccat_id = $value->gccat_id;
        $ccat_id = $value->ccat_id;
        $cat = $value->cat_id;

        if (isset($gccat_id) && $gccat_id == $id) {
            $dis_07 = $value->category;
            $dis_02 = $value->child_id;

        } 
        if (isset($ccat_id) && $ccat_id == $id) {
            $dis_03 = $value->category;
            $dis_02 = $value->parent_id;
        }
    }
}

$data['Dis_03'] = $dis_03;
$data['Dis_07'] = $dis_07;

if (isset($data['Dis_03'])) {
    echo $data['Dis_03'];
} 
if (isset($data['Dis_07'])) {
    echo $data['Dis_07'];
}

First I tried this way But In one I was getting right in second link I am getting right So Tried the code previous one .In the prvious I am getting correct and wrong one together EExample ZucchiniCauliflower

CODE 2

    if (isset($id)) {
        $db = Database::newInstance();
        $data = array();
        $data['cat_status'] = 1;
        $sql = "SELECT * FROM category WHERE cat_status=:cat_status ";
        $row = $db->read($sql,$data);
        $data['id'] = $crypt->decryptId($id);
        echo $data['id'];
        $id=$data['id'];
        if (is_array($row)) {
            foreach ($row as $value) {
                $gccat_id=$value->gccat_id;
                $ccat_id = $value->ccat_id;
                $cat = $value->cat_id;
    
                if (isset($gccat_id) && $gccat_id == $id) {
                    $data['Dis_03']=$value->category;
    
                } 
                if (isset($ccat_id) && $ccat_id == $id) {
                    $data['Dis_03'] = $value->category;
                    break;
                }
            }
        }
    }

--------------------------READ FROM HERE------------------------

Here is a link one when I click on this link

 $id=$value11->gccat_id;
 <a href="<?= BASEURL ?>ap/'.$value11->gccat_id.'">$title</a>

I expected the output is

Home>Raspberry

Here is a link Second link when I click on this link

Here id is ($value11->gccat_id)
window.open('<?= BASEURL ?>ap/'+id,'_self');

I expected the output is

Home>Cauliflower

1. WHEN I Use the Code 2 (Added break in this condition (isset($ccat_id) && $ccat_id == $id)) Then click on link second it gives output Home>Cauliflower which I was expecting. It is correct.

2. But this time as I added the break in (isset($ccat_id) && $ccat_id == $id). I click on link one It gives wrong output which I was not expecting. Home>Squash which is wrong.

In one link I was expecting

Home>Cauliflower

ERROR NOTE If I add Break; then link Second gives correct output but when I remove Break; then link one give correct. I wanted Both link should give correct output.

Madisson
  • 35
  • 5

1 Answers1

0

Problem was with cat_id,ccat_id ,gccat_id.

I provided 8 digit unique number with the following output,now I am getting the correct output.

function generateUniqueNumber() {
    return sprintf('%08d', mt_rand(1, 99999999));
}
Madisson
  • 35
  • 5
  • This is a good idea in principle. If you have unique IDs throughout then there's no danger of a collision. Having said that, mt_rand() could occasionally produce repeated numbers in theory, even with such a large range. You could consider generating GUIDs or UUIDs instead, which are _guaranteed_ to be unique. Right now, your numbers are only _likely_ to be unique. – ADyson Jan 04 '23 at 10:53
  • @ADyson Please read after the `RROR NOTE`, guide – Madisson Jan 04 '23 at 20:16
  • Yes I understand that. That wasn't the point of my comment. – ADyson Jan 04 '23 at 22:54
  • @ADyson I already made correction as you said mt_rand() produces repeated number , so as you said to generate GUIDs or UUIDs. I am asking addition question related to this check this https://stackoverflow.com/questions/75015906/how-to-show-parent-category-name-in-php – Madisson Jan 05 '23 at 09:14
  • If you've switched to using GUIDs, then why are you still asking that? It's still based on your old number system. – ADyson Jan 05 '23 at 09:17
  • @ADyson Check db image $cat_id=82477417 and parent_id=82477417 if both is equal then it is suppose to show parent category which is vegetables but showing Cauliflower. In previous question I was showing child category name which is `Home>Cauliflower` but now trying to show parent category name `Home>Vegetables>Cauliflower` – Madisson Jan 05 '23 at 09:38
  • Can you stop providing data as images please, you were told that a while back. See [Why not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question) if you don't understand why. It wasn't so bad when your IDs were only one digit, but now they're big strings it's a real pain to enter into a discussion about it...we don't want to have to type out the numbers again, and there's a danger of typing mistakes leading to confusion or errors. Provide the data as text then we can copy and paste – ADyson Jan 05 '23 at 10:31
  • https://stackoverflow.com/editing-help#tables shows you how to make a nicely-formatted table in the question – ADyson Jan 05 '23 at 10:32
  • @ADyson Can you help me with this new question ? https://stackoverflow.com/questions/75029083/how-to-use-where-as-function-and-select-function-in-pov-of-sql-injection?noredirect=1#comment132403234_75029083 – Madisson Jan 06 '23 at 09:50