Jump to content

Question about php echo and variables


mjurmann

Recommended Posts

Hello. I am trying to figure out if it is possible to create a loop with a counter and if the loop iteration is equal to a $_POST['x'] variable, then echo out information from a recordset that also includes the counter variable. This might not make much sense so just look at the code:

 

       	<?php $featureCount = 0;?> // initialize $featureCount counter
                    
                    	<?php do { ?> // being loop
                        
			<?php $featureCount++;?>	// add 1 to the variable $featureCount
                             
                            <?php if ($_POST['featureID1'] == $featureCount) { ?> // if posted number is equal to the $featureCount integer
                       
			 <?php echo $row_FeatureID;?><?php echo $featureCount;?><?php echo ['featureName']; ?> // echo the FeatureID query with the $featureCount current value added to the query name. I've created 20 different queries each ending with 1-20 (FeatureID1 - FeatureID20). Once the query is found with the number that matches the current $featureCount value, it will ech out the ['featureName'] field from that specific query. 
                                 
                                     	<?php } while ($featureCount < 20;?>
                        

 

So my question is, is this possible? I've been going at this all day long and can't get it. My head is ready to explode...please help :/

Link to comment
https://forums.phpfreaks.com/topic/59556-question-about-php-echo-and-variables/
Share on other sites

You want something like this? Also there is no need to leave and enter php each line.

 

<?php

$featureCount = 0;

do
{
// Is the posted number equal to the $featureCount?
if (isset($_POST['featureID1']) && (is_numeric($_POST['featureID1']) && $_POST['featureID1'] == $featureCount))
{
	// Get the data from the database
	$request = mysql_query("
		SELECT featureName
		FROM {TABLE NAME}
		WHERE FeatureID = $featureCount") or die(mysql_error());

	// Loop through the results and echo them.
	while ($row = mysql_fetch_assoc($request))
	{
		echo 'FeatureID = ' . $row['FeatureID'] . '<br />
			FeatureCount = ' . $featureCount . '<br />
			FeatureName = ' . $roe['featureName'] . '<br />';
	}
	mysql_free_result($request);
	$featureCount++;
}
while ($featureCount < 20);
?>

You are missing a substantial amount of code that defines several of your variables from your example...where does $row_FeatureID come from?  "echo ['featureName'];" isn't even valid syntax.

 

Why are you constantly going in and out of php?  E.g. <?php echo "a"; ?><?php echo "b"; ?> etc.  Just use one set of php tags...

 

<?php 
$featureCount = 0;

while ($featurecount < 20) [
$featureCount++;

if ($_POST['featureID1'] == $featureCount) {
	echo "featureID1 matches featureCount"; 
}

}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.