Jump to content

I need some help. Trying to show label before showing pictures in a row.


vet911
Go to solution Solved by Barand,

Recommended Posts

<?php
		try
		{
		  $type = $_GET['type'];
      
      if($_GET['type'] == 'ia')
				{
					
          $sql = "SELECT 
			groups.list,   
                        groups.title as label, 
                        images.image_th, 
                        images.image, 
                        images.cid, 
                        images.comment
			FROM groups
			INNER JOIN images
			ON images.list_images=groups.list
			WHERE list='ia' ORDER BY cid ASC";
				}
        
                 
			$result = $dbh->query($sql);
						
			if($result)
						
			{
				echo "<td height='60' colspan='3' width='800'>";
						echo "<hr />";
						echo "<div style='text-align: center;'><?php echo ['label']; ?></div>";
						echo "<div style='text-align: center;'>Click picture to enlarge.</div>";
						echo "<hr />";
						echo "</td>";
				// print_r($result);
				$i = 0;
				$max_columns = 3;
				foreach ($result as $row)
				{
					// open row if counter is zero
					if($i == 0)
					{
						echo "<tr VALIGN='top'>";
						
					}
          
					// make sure we have a valid product ALIGN='CENTER'

					if($row['image_th'] != "" && $row['image_th'] != null)
					{
						echo "<td><div><a href=$row[image] onclick='return hs.expand(this)' id='thumb1' class='highslide' title=$row[cid]><img src=$row[image_th] width=250></a> <div class='highslide-caption'>$row[comment]</div>";  
            echo " ";						
						if($row['cid'] != "" && $row['cid'] != null)
						{
							echo "<center><a>Item:</a>$row[cid]<br />";
                            
						}
						}
            
                 
	

			echo "</div></td>";
				
					// increment counter - if counter = max columns, reset counter and close row
					if(++$i == $max_columns)
					{
						echo "</tr>";
						$i=0;
					}	// end if
				} // end while
			} // end if results
      
      		
			if($i < $max_columns)
			{
				for($j=$i; $j<$max_columns;$j++)
				{
					echo "<td> </td>";
				}
			}
		}
		catch(PDOException $e)
		{
			echo $e->getMessage();
		}
		$dhb = null;
?>

I have tried several times, but can't figure it out as of yet. Some help would be appreciated.

Link to comment
Share on other sites

Check for changes in the title value. When a new value is encountered, output the new title.

 

pseudocode:

prev = '';
while (fetch next row) {
    if (title != prev) {
        output title
        prev = title                    // reset previous value
    }
    output image
}
Link to comment
Share on other sites

Thanks for the reply Barand.

I think I might have stated this incorrectly. I have a group of pictures that are in the linked in the database. They are in a group based on location. What I want is the location(title) to be displayed above the group of pictures. Only one per page and different per location.

Link to comment
Share on other sites

It may be that you would be better off putting the results into an explicitly defined multidimensional array:

 

$custResArray = array();
foreach($result as $row){
 $custResArray[$row['label'][$row['cid']] = array('list'=>$row['list'], 'image_th'=>$row['image_th'], 'image'=>$row['image'], 'commant'=>$row['comment']);
}

this will then give you an array where the top level is label, so you can then use that as a basis of rendering things on the page:

 

foreach($custResArray as $lbl=>$items){
echo $lbl;
foreach ($items as $cid=>$item){
echo $item['list'];
echo $item['image_th'];
...
echo $item['comment'];
}
}

 

this is just meant as an illustration, it's not supposed to work as is, but should give you what you need to work with.

Link to comment
Share on other sites

 

 

 

 

Thanks for the reply Muddy_Funster. I finally got a chance to work on this problem. I tried to incorporate your multidimensional array. Had some luck, had some failure.

I got it to work but it only shows item 14 in the browser when I run it. In the original form I was getting all picture to show. My objective was to make a file to show all picture in the database listed under the label. I have enclosed pictures of the displays(array.png shows current working on model.)(original.png shows before array work.  I appreciate you help. Looking for guidense to further this. My question is why don't I get all the data in the array I get it in the original sql statement as shown in the original picutre?

<?php
		try
		{
		  $type = $_GET['type'];
      
      if($_GET['type'] == 'dionne')
				{
					
          $sql = "SELECT 
					groups.list,
					groups.title as label,
					images.image_th,
					images.image,
					images.list_images,
					images.cid, images.comment
					FROM groups
					INNER JOIN images
					ON images.list_images=groups.list
					WHERE list='dionne' ORDER BY cid ASC";
				}    
                 
			$result = $dbh->query($sql);
			
			$custResArray = array();
			
			foreach($result as $row){
					$custResArray[$row['label']] = array('list_images'=>$row['list_images'],
					'image_th'=>$row['image_th'],
					'image'=>$row['image'],
					'cid'=>$row['cid'],
					'comment'=>$row['comment']);				
			}			
			if($result)
			{				
				// print_r($result);
				$i = 0;
				$max_columns = 3;
				
				foreach($custResArray as $lbl=>$items){
				echo "<hr />";
				echo "<div style='text-align: center;'>$lbl</div>";
				echo "<div style='text-align: center;'>Click picture to enlarge.</div>";
				echo "<hr />";
				
				foreach ($items as $cid=>$item)
				{ // while beginning
					// open row if counter is zero
					if($i == 0)
					{
						echo "<tr VALIGN='top'>";						
					}          
					if($row['image_th'] != "" && $row['image_th'] != null)
					{
						echo "<td><div><a href=$row[image] onclick='return hs.expand(this)' id='thumb1' class='highslide' title=$row[cid]><img src=$row[image_th] width=250></a> <div class='highslide-caption'>$row[comment]</div>";  
						echo " ";						
						if($row['cid'] != "" && $row['cid'] != null)
						{
							echo "<center><a>Item:</a>$row[cid]<br />";                         
						}
						}
						echo "</div></td>";				
					// increment counter - if counter = max columns, reset counter and close row
					if(++$i == $max_columns)
					{
						echo "</tr>";
						$i=0;
					}	// end if
				} // end while
			} // end if results
            		
			if($i < $max_columns)
			{
				for($j=$i; $j<$max_columns;$j++)
				{
					echo "<td> </td>";
				}
			}
		}
		}
		catch(PDOException $e)
		{
			echo $e->getMessage();
		}
		$dhb = null;
?>    
Link to comment
Share on other sites

You need to build an array for each label. You just keep overwriting the single element each time.

 

Try

					$custResArray[$row['label']][] = array('list_images'=>$row['list_images'],
					'image_th'=>$row['image_th'],
					'image'=>$row['image'],
					'cid'=>$row['cid'],
					'comment'=>$row['comment']);				

Note the added [] in $custResArray[$row['label']][]

Link to comment
Share on other sites

post-94675-0-41376200-1451243996_thumb.jpg

post-94675-0-63761600-1451243770_thumb.jpg

 

Thanks for the reply. I only want (1) label. the way I want it shown is in the pictures shown.(original1.jpg is from the original file posted. (array.jpg is what I am getting from the array) 

I don't know how to get all the data into the array. Help would be appreciated. No sure if the pictures will show up, the original posted were png file and they didnt show.

The label  "Gypsum City OHRV Park, ort Dodge, IA 2015" is for all the pictures below it.

Edited by vet911
Link to comment
Share on other sites

Thank you Barand for responding. I put the [ ] in place where you suggested. Now I have the exact number of pictures that are in the database under ia listing. But they are all the same #14.

I believe I have the multidimensional array code in the wrong place. Is this true?

 

post-94675-0-10113700-1451254019_thumb.jpg

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.