2017-10-14 1 views
1

Ich mache eine Suche auf der Grundlage des Namens des Produkts. Es funktioniert tatsächlich und es findet das Produkt nach der Suche, aber das Problem ist, wenn ich mehrere Produkte mit dem gleichen Namen habe, zeigt es mir nur die erste. Wie könnte es optimiert werden? Ich würde Ihre Hilfe schätzen.Suche mit PHP funktioniert nicht richtig

<?php 

//DATA coming from the browser 
$sSearch = $_GET['search']; 
//TURN it into UPPERCASE 
strtoupper($sSearch); 

//GETTING FROM FILE: 
$sajProducts = file_get_contents('products.txt'); 
$ajProducts = json_decode($sajProducts); 

$matchfound = false; 
//LOOPING THROUGH THE ARRAY OF PRODUCTS 
for ($i=0; $i< count($ajProducts); $i++) { 

    if ($sSearch == $ajProducts[$i]->name) { 
     $jSearchResult = $ajProducts[$i]; 
     $matchfound = true;  
     break; 
    } 
} 
//if there is a match display the product 
if ($matchfound) { 
    echo json_encode ($jSearchResult); 
    exit; 
} 
//if not display ALL products 
else { 

    echo json_encode($ajProducts); 
    exit; 
} 

?> 

Und die JSON products.text Datei:

[ 
    { 
     "id": "59d278cae7017", 
     "name": "A", 
     "price": "1", 
     "quantity": 3, 
     "image": "img_webshop\/productimage-59d74304917c2.jpg" 
    }, 
    { 
     "id": "59d27e20c8028", 
     "name": "A", 
     "price": "2", 
     "quantity": 3, 
     "image": "img_webshop\/productimage-59d743233c0cf.jpg" 
    }, 
    { 
     "id": "59d6a7ae16d15", 
     "name": "A", 
     "price": "3", 
     "quantity": 2, 
     "image": "img_webshop\/productimage-59d743392fbb5.jpg" 
    }, 
    { 
     "id": "59d6d6ee5f752", 
     "name": "A", 
     "price": "4", 
     "quantity": 4, 
     "image": "img_webshop\/productimage-59d74352d5b94.jpg" 
    }, 
    { 
     "id": "59d743d207bd5", 
     "name": "B", 
     "price": "5", 
     "quantity": 1, 
     "image": "img_webshop\/productimage-59d743d1e6e64.jpg" 
    }, 
    { 
     "id": "59d74451225ac", 
     "name": "B", 
     "price": "6", 
     "quantity": 1, 
     "image": "img_webshop\/productimage-59d7445120871.jpg" 
    }, 
    { 
     "id": "59e0d95cd4111", 
     "name": "B", 
     "price": "4", 
     "quantity": "5", 
     "image": "img_webshop\/productimage-59e0d95cd2c4b.jpg" 
    }, 
    { 
     "id": "59e0d992d1f3b", 
     "name": "C", 
     "price": "6", 
     "quantity": "5", 
     "image": "img_webshop\/productimage-59e0d992d19be.jpg" 
    }, 
    { 
     "id": "59e0d9c59fbf2", 
     "name": "D", 
     "price": "4", 
     "quantity": "5", 
     "image": "img_webshop\/productimage-59e0d9c59f1a5.jpg" 
    } 
] 

Antwort

0

Sammeln Sie alle übereinstimmenden Ergebnisse in einem Array und geben Sie sie dann in JSON zurück.

$jSearchResult = array(); 
for ($i=0; $i< count($ajProducts); $i++) { 

    if ($sSearch == $ajProducts[$i]->name) { 
     $jSearchResult[] = $ajProducts[$i]; 
     $matchfound = true;  
     //break; 
    } 
} 

So, hier nach foreach-Schleife die Iteration für alle json Werte abgeschlossen ist, werden wir alle endgültige Anordnung der Spiele in $jSearchResult

haben sie dann zurück:

if ($matchfound) { 
    echo json_encode ($jSearchResult); 
    exit; 
} 
+0

Danke @codeDragon für die Annahme meiner Antwort. Freue mich zu helfen. –

0

Mit diesem Code:

$matchfound = false; 
 
$jSearchResult = array(); 
 
//LOOPING THROUGH THE ARRAY OF PRODUCTS 
 
for ($i=0; $i< count($ajProducts); $i++) { 
 

 
    if ($sSearch == $ajProducts[$i]->name) { 
 
     $jSearchResult[] = $ajProducts[$i]; 
 
     $matchfound = true; 
 
    } 
 
}
Statt dessen:

$matchfound = false; 
 
//LOOPING THROUGH THE ARRAY OF PRODUCTS 
 
for ($i=0; $i< count($ajProducts); $i++) { 
 

 
    if ($sSearch == $ajProducts[$i]->name) { 
 
     $jSearchResult = $ajProducts[$i]; 
 
     $matchfound = true;  
 
     break; 
 
    } 
 
}

+0

euch danken. Es funktioniert jetzt perfekt. – codeDragon

+0

@codeDragon, vote meine Antwort bitte –

Verwandte Themen