Jump to content


Photo

decrementing


  • Please log in to reply
10 replies to this topic

#1 w00kie

w00kie
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 24 April 2006 - 03:34 PM

Hey, i have a DB with a list of gallery pictures.

Now i want a script to display and let users know how many pictures are in the dababase, which i used mysql_num_fields for. Since all the images are listed in one row, i couldn't use num_rows.

now i've tried to set up a script which will check for blank fields in the row, and if it has blank fields then it will decrement the number of images it echo's out


For instance, I have 10 images in my DB it echo's


"There are 10 images in the database"

Now, if i had 9 images, one blank field. I want it to say

" There are 9 images in the database"

So now it decrements down, and so fourth, Now i wanted to use && or, operators but my syntax is crap and i m not that very advanced at php, i would appreciate the help.


[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$num_rows = mysql_num_fields($result);

if( $e_1 && $e_2 == "") {


echo "There are : <B>";

echo --$num_rows;

echo "</b> Images in this group\n";

}
?>[/quote]

#2 w00kie

w00kie
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 24 April 2006 - 03:48 PM

Nevermind it seems i have solved my problem

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$num_rows = mysql_num_fields($result);

if($e_1=="") {


echo "There are : <B>";

--$num_rows;



if($e_2=="") {

--$num_rows;

}

if($e_3=="") {

echo --$num_rows;

}
}
?> [/quote]

#3 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 24 April 2006 - 03:54 PM

that seems to be a very odd way of going about it.

could you not just assign each group a unique number, and then SELECT * FROM table WHERE unique_number = $variable.

$images_in_group = mysql_num_rows($query);
wisewood: proven fact, I am both wise, and wooden.

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 April 2006 - 04:09 PM

It's a very strange way of doing things, but you can shorten your code considerably:
<?php
$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$num_rows = mysql_num_fields($result);
$row = mysql_fetch_assoc($result);
foreach($row as $k => $v)
     if ($v == '') $num_row--;
echo $num_rows;
?>

Ken

#5 w00kie

w00kie
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 24 April 2006 - 04:26 PM

[!--quoteo(post=368033:date=Apr 24 2006, 11:09 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 24 2006, 11:09 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
It's a very strange way of doing things, but you can shorten your code considerably:
<?php
$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$num_rows = mysql_num_fields($result);
$row = mysql_fetch_assoc($result);
foreach($row as $k => $v)
     if ($v == '') $num_row--;
echo $num_rows;
?>

Ken
[/quote]

Thanks for your responce ken, although i have images in e_2, e_5, e_6, yet it still displays '10'


#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 April 2006 - 04:35 PM

If you do
<?php
$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$row = mysql_fetch_assoc($result);
echo '<pre>' . print_r($row,true) . '</pre>';
?>
What is displayed? If you post that, we might be able to provide more assistance.

Ken

#7 w00kie

w00kie
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 24 April 2006 - 04:39 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]

Array
(
[e_1] => one.jpg
[e_2] =>
[e_3] =>
[e_4] =>
[e_5] =>
[e_6] =>
[e_7] =>
[e_8] =>
[e_9] =>
[e_10] =>
)
[/quote]

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 April 2006 - 05:12 PM

I created a test script with an array that matches what you posted.
<?php
$row = Array('e_1' => 'one.jpg', 'e_2' => '', 'e_3' => '', 'e_4' => '', 'e_5' => '', 'e_6' => '', 'e_7' => '', 'e_8' => '', 'e_9' => '', 'e_10' => '');
$num_rows = count($row);
echo $num_rows.'<br>';
foreach($row as $k=>$v)
    if ($v == '') $num_rows--;
echo $num_rows;
?>

When I run this script, it shows
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]10
1[/quote]

Which is what I expect. Do you have any other code in your script you're not posting?

Ken

#9 w00kie

w00kie
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 24 April 2006 - 05:18 PM

No Ken, i ran the php script in a seperate PHP file.

When i run your array script i get 10, 1 also.

but when i run the script you previously posted, it results as 10.

even though now i've added .jpgs in e_1, e_2, e_3, e_4 of the database.

& also, I dont know if this helps but i previously stated that the images were all on the same row, not individual rows, i dont know if that would change anything.

#10 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 24 April 2006 - 05:53 PM

I think I found the problem... Just a minor misspelled variable in one of my examples... :-(

<?php
$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$num_rows = mysql_num_fields($result);
$row = mysql_fetch_assoc($result);
foreach($row as $k => $v)
     if ($v == '') $num_rows--; // I had $num_row originally
echo $num_rows;
?>

You can also just add up the entries which are not blank:
<?php
$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$num_rows = 0
$row = mysql_fetch_assoc($result);
foreach($row as $k => $v)
     if ($v != '') $num_rows++;
echo $num_rows;
?>

Ken

#11 w00kie

w00kie
  • Members
  • Pip
  • Newbie
  • 9 posts

Posted 24 April 2006 - 05:57 PM

[!--quoteo(post=368075:date=Apr 24 2006, 12:53 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 24 2006, 12:53 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I think I found the problem... Just a minor misspelled variable in one of my examples... :-(

<?php
$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$num_rows = mysql_num_fields($result);
$row = mysql_fetch_assoc($result);
foreach($row as $k => $v)
     if ($v == '') $num_rows--; // I had $num_row originally
echo $num_rows;
?>

You can also just add up the entries which are not blank:
<?php
$result = mysql_query("SELECT e_1, e_2, e_3, e_4, e_5, e_6, e_7, e_8, e_9, e_10 FROM f_images", $link);
$num_rows = 0
$row = mysql_fetch_assoc($result);
foreach($row as $k => $v)
     if ($v != '') $num_rows++;
echo $num_rows;
?>

Ken
[/quote]

Thank you Ken, and thank you for being so patient <3





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users