Jump to content

Archived

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

w00kie

decrementing

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites
It's a very strange way of doing things, but you can shorten your code considerably:
[code]<?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;
?>[/code]

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=368033:date=Apr 24 2006, 11:09 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Apr 24 2006, 11:09 AM) [snapback]368033[/snapback][/div][div class=\'quotemain\'][!--quotec--]
It's a very strange way of doing things, but you can shorten your code considerably:
[code]<?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;
?>[/code]

Ken
[/quote]

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

Share this post


Link to post
Share on other sites
If you do
[code]<?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>';
?>[/code]
What is displayed? If you post that, we might be able to provide more assistance.

Ken

Share this post


Link to post
Share on other sites
[!--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]

Share this post


Link to post
Share on other sites
I created a test script with an array that matches what you posted.
[code]<?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;
?>[/code]

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I think I found the problem... Just a minor misspelled variable in one of my examples... :-(

[code]<?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;
?>[/code]

You can also just add up the entries which are not blank:
[code]<?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;
?>[/code]

Ken

Share this post


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

[code]<?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;
?>[/code]

You can also just add up the entries which are not blank:
[code]<?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;
?>[/code]

Ken
[/quote]

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

Share this post


Link to post
Share on other sites

×

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.