johnyp Posted July 22, 2012 Share Posted July 22, 2012 Hello to all again. I asked the same question again in the past but i did not manage to find any solutions yet. I will post the file with the variables i am using now and what i need is to loop from 1 to 15 for fields make, typeof vehicle and fetures and load them in a variable name "$make_category number here" , "$typeofvehicle_category number here" and "$features_category number here". Please take a look and advice but please don't mention to use arrays. I can't. Thank you. <?php include 'settings.php'; $con = mysql_connect($localhost,$username,$password); mysql_select_db($database, $con); // <!-- ==================================================================================================== --> // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM make where catid = 1"); while($r = mysql_fetch_assoc($results)) { $make_car.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM make where catid = 2"); while($r = mysql_fetch_assoc($results)) { $make_bike.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM make where catid = 3"); while($r = mysql_fetch_assoc($results)) { $make_van.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM make where catid = 4"); while($r = mysql_fetch_assoc($results)) { $make_truck.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> // <!-- ==================================================================================================== --> $results = mysql_query("SELECT * FROM typeofvehicle where catid = 1"); while($r = mysql_fetch_assoc($results)) { $typeofvehicle_car.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM typeofvehicle where catid = 2"); while($r = mysql_fetch_assoc($results)) { $typeofvehicle_bike.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM typeofvehicle where catid = 3"); while($r = mysql_fetch_assoc($results)) { $typeofvehicle_van.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM typeofvehicle where catid = 4"); while($r = mysql_fetch_assoc($results)) { $typeofvehicle_truck.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> // <!-- ==================================================================================================== --> $results = mysql_query("SELECT * FROM doors"); while($r = mysql_fetch_assoc($results)) { $doors.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM gearbox"); while($r = mysql_fetch_assoc($results)) { $gearbox.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $fueltype.= "<option>Πετρέλαιο</option>"; $fueltype.= "<option>Βενζίνη</option>"; $fueltype.= "<option>Αέριο</option>"; $fueltype.= "<option>Ηλεκτρικό</option>"; $fueltype.= "<option>Υδρογόνο</option>"; $fueltype.= "<option>Υβριδικό</option>"; // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM extcolor"); while($r = mysql_fetch_assoc($results)) { $extcolor.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM climaticcontrol"); while($r = mysql_fetch_assoc($results)) { $climaticcontrol.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=0; while($i <= 5000) { $liter.= "<option value='$i'>$i</option>"; $i = $i+100; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=2012; while($i >= 1990) { $year.= "<option value='$i'>$i</option>"; $i = $i-1; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=0; while($i <= 1000000) { $miles.= "<option value='$i'>$i</option>"; $i = $i+500; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=0; while($i <= 200000) { $price.= "<option value='$i'>$i</option>"; $i = $i+500; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=0; while($i <= 500) { $power.= "<option value='$i'>$i</option>"; $i = $i+10; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 1"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_car_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_car_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_car_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 2"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_bike_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_bike_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_bike_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 3"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_van_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_van_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_van_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 4"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_truck_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_truck_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_truck_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 5"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_bus_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_bus_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_bus_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 6"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_6_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_6_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_6_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 7"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_7_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_7_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_7_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 8"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_8_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_8_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_8_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 9"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_9_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_9_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_9_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 10"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_10_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_10_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_10_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 11"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_11_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_11_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_11_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 12"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_12_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_12_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_12_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 13"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_13_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_13_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_13_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 14"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_14_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_14_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_14_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM features where catid = 15"); $num_rows = mysql_num_rows($results); $ii = $num_rows/3; $ii = round($ii, 0, PHP_ROUND_HALF_UP); $i = 1; while($r = mysql_fetch_assoc($results)) { if ($i > 00 && $i <= $ii){$features_15_1.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){$features_15_2.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){$features_15_3.= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/ Share on other sites More sharing options...
scootstah Posted July 22, 2012 Share Posted July 22, 2012 Please take a look and advice but please don't mention to use arrays. I can't. Why can't you? If you can access $typeofvehicle_category_15 then you can access $typeofvehicle_category[15], which is a much better solution. Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363466 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2012 Share Posted July 22, 2012 A) The reason programmers keep mentioning arrays is because you have sets of data and that is exactly what arrays are for, i.e. sets of data. YOU CAN and should be using arrays. B) Your code is horrific. You should not be executing all those separate query statements to get the result for each hard-coded value in sequence, nor should you have variables like $make_car, $make_bike, $make_van holding the result. I would estimate you can reduce the amount of code to about 1/10 of what you have and you haven't even shown the code where you are using the information. The goal of code is to be general purpose, so that you don't need to keep copy/pasting and altering sections just because you add a new category of data. A specific example of what you SHOULD be doing would be to get all the make information using one query, in the order that you want it. SELECT * FROM make ORDER BY catid You would then simply iterate over that complete result set and output it the way you want and if you do happen to need to reference each catid value separately, you would simply store the result set in an intermediate array using the catid as the array index so that you can access any group of catid rows as needed. Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363472 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2012 Share Posted July 22, 2012 I think the reason you think you need to put the data into variables like $typeofvehicle_category_15 is because you later have more hard-coded logic, where you have written out code for each possible result section, rather than to simply loop over the data and let the computer output each section as the category/features/option value changes or to reference just the category/features/option value that you want where you want. If you would post your code that is actually using the data, I'm sure several people would jump on the opportunity to show how you can greatly simplify what you are doing. Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363478 Share on other sites More sharing options...
PFMaBiSmAd Posted July 22, 2012 Share Posted July 22, 2012 Here's some sample code that shows what we have been suggesting. It replaces your 15 sets of features code with one general purpose block of code that doesn't care how many sets of features there are or what they are named. Everything is in terms of the catid value. All you need to do to change anything is add/subtract the data from the database and alter the $cat array that supplies the 'title' for the data section (which itself should be data in a database table that can can be gotten via a query.) You don't need to copy/paste/alter code 15 times just because the data value changes. // list of catid values/titles $cat = array(1=>'Cars',2=>'Bikes',3=>'Vans',4=>'Trucks',5=>'Buses',6=>'',7=>'',8=>'',9=>'',10=>'',11=>'',12=>'',13=>'',14=>'',15=>''); // get the features data $result = mysql_query("SELECT * FROM features ORDER BY catid,name"); // you probably want the features to be alphabetical by name under each catid $feature_data = array(); while($row = mysql_fetch_assoc($result)){ $feature_data[$row['catid']][] = $row; // store the data using the catid as the index } // produce checkboxes from the data $features = array(); foreach($feature_data as $catid=>$arr){ $num_rows = count($arr); $ii = $num_rows/3; //$ii = round($ii, 0, PHP_ROUND_HALF_UP); $ii = ceil($ii); $i = 1; foreach($arr as $r){ if ($i > 00 && $i <= $ii){if(!isset($features[$catid][1])){$features[$catid][1]='';} $features[$catid][1].= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){if(!isset($features[$catid][2])){$features[$catid][2]='';}$features[$catid][2].= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){if(!isset($features[$catid][3])){$features[$catid][3]='';}$features[$catid][3].= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } } // at the point of outputting the features for any particular catid, for example 3='Vans' $catid = 3; // gotten (typically from a loop) for the catid you are currently outputting the data for echo "Feature options for {$cat[$catid]} -<br />"; echo "<table><tr>"; foreach($features[$catid] as $column){ echo "<td>$column</td>\n"; } echo "</tr></table>"; Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363496 Share on other sites More sharing options...
johnyp Posted July 23, 2012 Author Share Posted July 23, 2012 You are making it look so simple... I will give it a try cause i see that array is a must in my situation. You are all so helpfull here. I think i will learn a lot of you! Thank you all i will post my changes soon. Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363623 Share on other sites More sharing options...
peipst9lker Posted July 23, 2012 Share Posted July 23, 2012 You might consider reading a book about PHP to learn those very basic things from scratch? Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363650 Share on other sites More sharing options...
johnyp Posted July 23, 2012 Author Share Posted July 23, 2012 Oh i am from those persons that learn from examples and real life use. Anyway here is how i use those vars later: <TABLE style="width:100%;color:blue" border="1" bordercolor="red"> <tr style="vertical-align:top"> <td style=width:33%> <?php echo($features_car_1); ?> </td> <td style=width:33%> <?php echo($features_car_2); ?> </td> <td style=width:33%> <?php echo($features_car_3); ?> </td> </tr> </TABLE> </br> <center> <INPUT TYPE="button" class="button" value="<< Προηγούμενο" onClick="hideDiv('features'); showDiv('dropdowns');"> <INPUT TYPE="submit" class="button" value="Αναζήτηση"> </br> <!-- <?php echo "Number of features found: " . $num_rows; ?> --> </center> I have a page for every category that load in a div in main category search page. Should i change <?php echo($features_car_1); ?> to <?php echo($features[1][1]); ?> maybe ? Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363664 Share on other sites More sharing options...
scootstah Posted July 23, 2012 Share Posted July 23, 2012 No, it is an array - use loops. foreach($features_car as $car) { echo '<td style="width:33%">' . $car . '</td>'; } Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363686 Share on other sites More sharing options...
johnyp Posted July 23, 2012 Author Share Posted July 23, 2012 Diar scootstah i solved that issue cause i have made the loop as PFMaBiSmAd has shown me after got the values to the array. Here is my new variables file: <?php include 'settings.php'; $con = mysql_connect($localhost,$username,$password); mysql_select_db($database, $con); // <!-- ==================================================================================================== --> $i=0; while($i <= 5000) { $liter.= "<option value='$i'>$i</option>"; $i = $i+100; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=2012; while($i >= 1990) { $year.= "<option value='$i'>$i</option>"; $i = $i-1; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=0; while($i <= 1000000) { $miles.= "<option value='$i'>$i</option>"; $i = $i+500; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=0; while($i <= 200000) { $price.= "<option value='$i'>$i</option>"; $i = $i+500; } // <!-- ---------------------------------------------------------------------------------------------------- --> $i=0; while($i <= 500) { $power.= "<option value='$i'>$i</option>"; $i = $i+10; } // <!-- ---------------------------------------------------------------------------------------------------- --> function cField(cmin,cmax,cstep) $i=cmin; while($i <= cmax) { $cField.= "<option value='$i'>$i</option>"; $i = $i+cstep; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM doors"); while($r = mysql_fetch_assoc($results)) { $doors.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM gearbox"); while($r = mysql_fetch_assoc($results)) { $gearbox.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $fueltype.= "<option>Πετρέλαιο</option>"; $fueltype.= "<option>Βενζίνη</option>"; $fueltype.= "<option>Αέριο</option>"; $fueltype.= "<option>Ηλεκτρικό</option>"; $fueltype.= "<option>Υδρογόνο</option>"; $fueltype.= "<option>Υβριδικό</option>"; // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM extcolor"); while($r = mysql_fetch_assoc($results)) { $extcolor.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $results = mysql_query("SELECT * FROM climaticcontrol"); while($r = mysql_fetch_assoc($results)) { $climaticcontrol.= "<option value='$r[id]'>$r[name]</option>"; } // <!-- ---------------------------------------------------------------------------------------------------- --> $result = mysql_query("SELECT * FROM make"); $make_data = array(); while($row = mysql_fetch_assoc($result)){ $make_data[$row['catid']][] = $row; } $make = array(); foreach($make_data as $catid=>$arr){ $num_rows = count($arr); foreach($arr as $r){ $make[$catid].= "<option value='$r[id]'>$r[name]</option>"; } } // <!-- ---------------------------------------------------------------------------------------------------- --> $result = mysql_query("SELECT * FROM typeofvehicle"); $typeofvehicle_data = array(); while($row = mysql_fetch_assoc($result)){ $typeofvehicle_data[$row['catid']][] = $row; } $typeofvehicle = array(); foreach($typeofvehicle_data as $catid=>$arr){ $num_rows = count($arr); foreach($arr as $r){ $typeofvehicle[$catid].= "<option value='$r[id]'>$r[name]</option>"; } } // <!-- ---------------------------------------------------------------------------------------------------- --> $result = mysql_query("SELECT * FROM features"); $feature_data = array(); while($row = mysql_fetch_assoc($result)){ $feature_data[$row['catid']][] = $row; } $features = array(); foreach($feature_data as $catid=>$arr){ $num_rows = count($arr); $ii = $num_rows/3; //$ii = round($ii, 0, PHP_ROUND_HALF_UP); $ii = ceil($ii); $i = 1; foreach($arr as $r){ if ($i > 00 && $i <= $ii){if(!isset($features[$catid][1])){$features[$catid][1]='';} $features[$catid][1].= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii && $i <= $ii*2){if(!isset($features[$catid][2])){$features[$catid][2]='';}$features[$catid][2].= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} if ($i > $ii*2 && $i <= $ii*3){if(!isset($features[$catid][3])){$features[$catid][3]='';}$features[$catid][3].= "<input type='checkbox' name='features($r[id])' value='1' />$r[name] </br>";} $i++; } } // <!-- ---------------------------------------------------------------------------------------------------- --> mysql_close(); ?> My question now is how to make a function that its name will be the var name also? For example: function cField(cmin,cmax,cstep) $i=cmin; while($i <= cmax) { $cField.= "<option value='$i'>$i</option>"; $i = $i+cstep; } and then use it like this : <select name="miles" id="miles" style=width:68px> <option value="">...</option> <?php echo $cField(0,999999,500); ?> </select> and that will give me a dropdown with values from 0 to 999999 with 500 step up. Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363690 Share on other sites More sharing options...
Barand Posted July 23, 2012 Share Posted July 23, 2012 1. Your function definition needs to be enclosed in {...} 2. To echo the result of the function it needs to return the value. 3. php variables begin with $ so function cField($cmin,$cmax,$cstep) { $i=$cmin; $cField = ''; while($i <= $cmax) { $cField.= "<option value='$i'>$i</option>"; $i = $i+$cstep; } return $cField; } Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363691 Share on other sites More sharing options...
johnyp Posted July 23, 2012 Author Share Posted July 23, 2012 Thank you Barand. I saw my {...} error. Is this use later <?php echo $cField(0,999999,500); ?> ok? Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363700 Share on other sites More sharing options...
scootstah Posted July 23, 2012 Share Posted July 23, 2012 Remove the $. echo cField(0,999999,500); Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363704 Share on other sites More sharing options...
johnyp Posted July 23, 2012 Author Share Posted July 23, 2012 Yes thank you that works fine and i assume that if i go echo cField(999999,0,-500); It will go backwards despite i have $i = $i+$cstep; while the $cstep is negative. Right? Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363708 Share on other sites More sharing options...
scootstah Posted July 23, 2012 Share Posted July 23, 2012 That wouldn't do anything, since $cmin is already greater than $cmax. Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363711 Share on other sites More sharing options...
johnyp Posted July 23, 2012 Author Share Posted July 23, 2012 Yep... Two functions is ok i think... Thank you Quote Link to comment https://forums.phpfreaks.com/topic/266077-get-sql-values-and-make-var-name-dynamically/#findComment-1363724 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.