budimir Posted January 8, 2015 Share Posted January 8, 2015 I' stuck with writing function for searching replaced numbers and would really appreciate if someone can tell me what I'm doing wrong. My table structure looks like: CREATE TABLE servis.zamjene_brojeva ( id INT(11) NOT NULL AUTO_INCREMENT, vrijeme TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, pocetni_broj VARCHAR(55) DEFAULT NULL, zamjenski_broj VARCHAR(55) DEFAULT NULL, glavni_broj VARCHAR(55) DEFAULT NULL, postoji_zamjena INT(1) DEFAULT NULL, PRIMARY KEY (id) ) My function looks like this: //funkcija za traženje zamjene brojeva function zamjena_broja($kataloski_broj){ //Traženje zamjenskog broja $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE glavni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); $row = mysql_fetch_array($rezultat_zamjena); $kataloski_broj = $row["zamjenski_broj"]; $broj_zamjena = mysql_num_rows($rezultat_zamjena); return $kataloski_broj; //Traženje druge zamjene broja if ($broj_zamjena <> 0) { $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE pocetni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); $broj_zamjena2 = mysql_num_rows($rezultat_zamjena); $row2 = mysql_fetch_array($rezultat_zamjena); $kataloski_broj = $row2["zamjenski_broj"]; return $kataloski_broj; //Traženje treće zamjene if ($broj_zamjena2 <> 0) { $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE pocetni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); $broj_zamjena3 = mysql_num_rows($rezultat_zamjena); $row3 = mysql_fetch_array($rezultat_zamjena); $kataloski_broj = $row3["zamjenski_broj"]; return $kataloski_broj; } } } In pocetni_broj is old number and in glavni_broj is new number. But it can happen that number in glavni_broj is old and I need to search in pocetni_broj to see if there is even newer number and it can happen 5 or 6 times like that. I need to find all the numbers that are connected, but I'm not getting that. What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/ Share on other sites More sharing options...
Barand Posted January 8, 2015 Share Posted January 8, 2015 (edited) It looks as though that function will never get past line 14, where it just returns the value of $kataloski_broj that you passed to it. You could save time by having function zamjena_broja($kataloski_broj){ return $kataloski_broj; } How do you know if a number is newer? Is it freshly painted whereas the old ones are a little rusty? You have a timestamp field (vrijeme) but you aren't using that in any of your queries. Perhaps if you showed us some sample data to demonstrate exactly what you are looking for. Edited January 8, 2015 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502155 Share on other sites More sharing options...
budimir Posted January 8, 2015 Author Share Posted January 8, 2015 Hey Barand, Thanks for you're time and effort: Here is example how data looks like in table. pocetni_broj zamjenski_broj glavni_broj 503 86 27-02 544 02 83-01 544 02 83-01503 86 27-03 544 02 83-01 544 02 83-01503 86 28-03 544 02 83-01 544 02 83-01 I can't use vrijeme because that is only info when that data was inserted in table. Basiclly, the logic should be when I can't find glavni_broj which has pocetni broj or vice versa than function stops. I need to link all connected numbers. In upper example those are numbers: 503 86 27-02, 544 02 83-01, 503 86 27-03, 503 86 28-03. I don't know how to get them all out with that function. Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502159 Share on other sites More sharing options...
Barand Posted January 8, 2015 Share Posted January 8, 2015 vrijeme is set when data is inserted and is updated every time the data in the record is changed. Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502163 Share on other sites More sharing options...
budimir Posted January 9, 2015 Author Share Posted January 9, 2015 Yes, that is correct. Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502236 Share on other sites More sharing options...
Barand Posted January 9, 2015 Share Posted January 9, 2015 Still cannot understand exactly what is required. How do you call the function and where does the value for $kataloski_broj come from that you pass to the function? Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502239 Share on other sites More sharing options...
budimir Posted January 9, 2015 Author Share Posted January 9, 2015 Hey Barand, Let me explain the situation I need to solve. I have one article which is comeing under different part numbers (same product with different article numbers). I need to somehow get all linked numbers and use last glavni_broj to be able to identify the stock. So in the previous example that would be: Linked numbers: 503 86 27-02, 544 02 83-01, 503 86 27-03, 503 86 28-03 Main number: 544 02 83-01 Then I could use all these linked numbers to sum the stock and show to the user and also redirect him to newest number for order. That's where I can't find a solution. How to display all linked numbers. Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502242 Share on other sites More sharing options...
budimir Posted January 9, 2015 Author Share Posted January 9, 2015 Barand, I have made it to get the result I need and now the function looks like this. //Traženje zamjenskog broja $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE glavni_broj = '$kataloski_broj' OR pocetni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); while($row = mysql_fetch_array($rezultat_zamjena)){ $kataloski_broj_g = $row["zamjenski_broj"]; $kataloski_broj_p = $row["pocetni_broj"]; $broj_zamjena = mysql_num_rows($rezultat_zamjena); echo "Glavni broj A - $kataloski_broj_g<br> Početni broj A - $kataloski_broj_p<br> Broj zamjena A - $broj_zamjena<br>"; } //Traženje zamjene if ($broj_zamjena == 1) { $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE glavni_broj = '$kataloski_broj_g'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); while($row3 = mysql_fetch_array($rezultat_zamjena)) { $kataloski_broj_g2 = $row3["zamjenski_broj"]; $kataloski_broj_p2 = $row3["pocetni_broj"]; $broj_zamjena3 = mysql_num_rows($rezultat_zamjena); echo "Glavni broj C - $kataloski_broj_g2<br> Početni broj C - $kataloski_broj_p2<br> Broj zamjena C - $broj_zamjena<br>"; } } return $kataloski_broj; } Only problem now is how to output those linked numbers and main number in return $kataloski_broj. Can you help me with that? Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502251 Share on other sites More sharing options...
Barand Posted January 9, 2015 Share Posted January 9, 2015 This will give you the newest record for a given value for glavni_broj $kataloski_broj = '544 02 83-01'; $sql = "SELECT z.vrijeme , pocetni_broj , zamjenski_broj , glavni_broj FROM zamjene_brojeva z JOIN ( SELECT glavni_broj , MAX(vrijeme) as vrijeme FROM zamjene_brojeva GROUP BY glavni_broj ) x USING (glavni_broj, vrijeme) WHERE glavni_broj = '$kataloski_broj'"; Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502254 Share on other sites More sharing options...
budimir Posted January 9, 2015 Author Share Posted January 9, 2015 Barand, This is the final result I need. I managed to link them in an array. function zamjena_broja($kataloski_broj){ $kataloski_broj_g = array(); $kataloski_broj_p = array(); //Traženje zamjenskog broja $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE glavni_broj = '$kataloski_broj' OR pocetni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); while($row = mysql_fetch_array($rezultat_zamjena)){ $kataloski_broj_g['G'] = $row["zamjenski_broj"]; $kataloski_broj_p['Z'] = $row["pocetni_broj"]; $broj_zamjena = mysql_num_rows($rezultat_zamjena); /* echo "Glavni broj A - $kataloski_broj_g<br> Početni broj A - $kataloski_broj_p<br> Broj zamjena A - $broj_zamjena<br>"; */ //Ovdje su svi zamjenski brojevi print_r($kataloski_broj_p); } //Ovdje je u uvijek jedan glavni broj print_r($kataloski_broj_g); //Ovo je ispis koji baca funkcija return $kataloski_broj_p; return $kataloski_broj_g; } Can you tell me how to have sam result from: print_r($kataloski_broj_p);print_r($kataloski_broj_g); in return $kataloski_broj_p; return $kataloski_broj_g; That is only problem I'm facing now. Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502263 Share on other sites More sharing options...
Solution Barand Posted January 9, 2015 Solution Share Posted January 9, 2015 Return a single array containing two sub-arrays function zamjena_broja($kataloski_broj){ $kataloski_broj_data = array(); //Traženje zamjenskog broja $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE glavni_broj = '$kataloski_broj' OR pocetni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); while($row = mysql_fetch_array($rezultat_zamjena)){ $kataloski_broj_data['G'][] = $row["zamjenski_broj"]; $kataloski_broj_data['Z'][] = $row["pocetni_broj"]; } return $kataloski_broj_data; } 1 Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502265 Share on other sites More sharing options...
budimir Posted January 9, 2015 Author Share Posted January 9, 2015 I have tried it and returns nothing array is empty. This is how I reworked the function. function zamjena_broja($kataloski_broj){ $kataloski_broj_data = array(); //Traženje zamjenskog broja $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE glavni_broj = '$kataloski_broj' OR pocetni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); while($row = mysql_fetch_array($rezultat_zamjena)){ $kataloski_broj_data['G'][] = $row["zamjenski_broj"]; $kataloski_broj_data['Z'][] = $row["pocetni_broj"]; /* echo "Glavni broj A - $kataloski_broj_g<br> Početni broj A - $kataloski_broj_p<br> Broj zamjena A - $broj_zamjena<br>"; */ //Ovdje su svi zamjenski brojevi //print_r($kataloski_broj_p); } //Ovdje je u uvijek jedan glavni broj //print_r($kataloski_broj_g); //Ovo je ispis koji baca funkcija return $kataloski_broj_data; } Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502267 Share on other sites More sharing options...
budimir Posted January 9, 2015 Author Share Posted January 9, 2015 Barand, I have almost made it. I have this code: //funkcija za traženje zamjene brojeva function zamjena_broja($kataloski_broj){ $kataloski_broj_data = array(); //Traženje zamjenskog broja $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE glavni_broj = '$kataloski_broj' OR pocetni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); while($row = mysql_fetch_array($rezultat_zamjena)){ $kataloski_broj_data[] = $row["zamjenski_broj"]; $kataloski_broj_data[] = $row["pocetni_broj"]; } $kataloski_broj = $input = array_map("unserialize", array_unique(array_map("serialize", $kataloski_broj_data))); var_dump($kataloski_broj); //Ovo je ispis koji baca funkcija return $kataloski_broj; } On var_dump($kataloski_broj) I get this value: array (size=4) 0 => string '544 02 83-01' (length=12) 1 => string '503 86 27-02' (length=12) 3 => string '503 86 27-03' (length=12) 5 => string '503 86 28-03' (length=12) Which is totally OK. Now my problem is that on return I get array. This is how I test my function: <form method="GET" action="<?php $_SERVER["PHP_SELF"]; ?>"> <input type="text" name="kataloski" value="" /> <input type="submit" name="submit" value="Trazi" /> </form> <?php $kataloski = zamjena_broja($_GET["kataloski"]); echo "<br>Kataloški koji tražimo - $kataloski<br> Pronađeni kataloški:<br> "; //Pregled array ?> What am I doing wrong that function is not returning values, but Array??? Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502278 Share on other sites More sharing options...
budimir Posted January 9, 2015 Author Share Posted January 9, 2015 Thanks Barand! You have pointed me to the right direction. I have solved my problem. Thank you very much. This is the function and whole script how I tested it if anyone needs it. <?php include ("../../admin/servis/include/session.php"); //funkcija za traženje zamjene brojeva function zamjena_broja($kataloski_broj){ $kataloski_broj_data = array(); //Traženje zamjenskog broja $upit_zamjena = "SELECT pocetni_broj, zamjenski_broj, glavni_broj FROM zamjene_brojeva WHERE glavni_broj = '$kataloski_broj' OR pocetni_broj = '$kataloski_broj'"; $rezultat_zamjena = mysql_query($upit_zamjena) or die (mysql_error()); while($row = mysql_fetch_array($rezultat_zamjena)){ $kataloski_broj_data[] = $row["zamjenski_broj"]; $kataloski_broj_data[] = $row["pocetni_broj"]; } $kataloski_broj = array_map("unserialize", array_unique(array_map("serialize", $kataloski_broj_data))); var_dump($kataloski_broj); return $kataloski_broj; } ?> <form method="GET" action="<?php $_SERVER["PHP_SELF"]; ?>"> <input type="text" name="kataloski" value="" /> <input type="submit" name="submit" value="Trazi" /> </form> <?php $kataloski = zamjena_broja($_GET["kataloski"]); foreach ($kataloski as $value) { echo "$value<br>"; } /* echo $kataloski[0]; echo "<br>"; echo $kataloski[1]; echo "<br>"; echo $kataloski[3]; echo "<br>"; echo $kataloski[5]; */ ?> Quote Link to comment https://forums.phpfreaks.com/topic/293752-function-to-search-for-replaced-numbers/#findComment-1502281 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.