2

I have this while loop in php:

<?php while($percentSKU = sqlsrv_fetch_array($resultSKU, SQLSRV_FETCH_ASSOC)) : ?>
    <p><?php echo $percentSKU['PercentageSales']; ?>%</p>
<?php endwhile; ?>

Which out puts 12 rows, for example:

.00%, .00%, .00%, .00%, 26.22%, .00%, 3.96%, 1.49%, 5.23%, 20.88%, 42.22%, .00%

Is there a way to only print out 1 particular row?

I have tried things like this:

<?php echo $percentSKU['PercentageSales'][4]; ?>

But because the the array returns a sting, I am getting the nth single character not the nth row of data. So instead of getting '26.22' im am getting '2'

Any ideas?

4
  • What about just use echo $percentSKU['PercentageSales']? I'm not sure as I can't see how the array looks like. Could you use print_r($precentSKU['PercentageSales']); and show us the output? Commented Nov 22, 2016 at 9:27
  • 1
    What is the condition for choosing the row to output? Commented Nov 22, 2016 at 9:27
  • <?php print_r($percentSKU['PercentageSales']); ?> just gives me '.00%' which i am assuming is just giving me the first row... Commented Nov 22, 2016 at 9:30
  • I'm confuse why it's 12 rows not cols? what's your table structure? Commented Nov 22, 2016 at 9:44

2 Answers 2

2

If it is returning a string you can convert it to an array and access it via its index. To convert a string to an array you can use the explode function.

for ex :

$string = '.00%, .00%, .00%, .00%, 26.22%, .00%, 3.96%, 1.49%, 5.23%, 20.88%, 42.22%, .00%';

$result = explode(',' $string);
$finalvalue = $result[0];

Note : The $result[0] will return the first value of the string, you will have to set the index which you require.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks that is great! So if i was to take this one step further and make $string = $percentSKU['PercentageSales']; to make it dynamic how would i go about doing that because i don't get any output with that at the moment. would i need to be wrapped in a loop?
Try doing var_dump to the $percentSKU['PercentageSales']; variable and try to find are you getting the value in your $resultSKU first or not.
var_dump gives me this: string(3) ".00"
Yes..so that means you are getting the value.. try doing print_r($yourvariable) and then choose the index whose value you require and echo it.
0

working with mysql I use that function:

function ExecuteQuery_assocArray($query) 
{
  $result = mysqli_query($this->link, $query);
  $set = array();
  while ($set[] = mysqli_fetch_assoc($result)); 
  return $set;
}

it returns $set of rows

3 Comments

Thanks for taking the time to comment, but i am using SQL Server not MySQL so unfortunately this wouldn't be usable for myself.
@PHPNewbie, but you didn't specify on your question nor on your tag that you're using SQL server.
@Ronald, i did but someone edit my question to remove the tag.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.