Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 100
  • Last Modified:

Call custom function in php and arrange data as attach file

Hello;

I retrieve data with a query as a custom function as below:

QUERY

 <?php
		 
	 $query_artandculture = "select tbarticles.articleid,tbarticles.title,substr(tbarticles.articlebody,1,200) as description1,tbphotos.photofilename,tbarticles.deletitem ,tbarticlecategories.catid,tbcategories.parentid,tbarticles.mediastate,tbphotos.altdescription
	 
from  tbarticles 

INNER JOIN tbphotos on tbphotos.articleid = tbarticles.articleid 
INNER JOIN tbarticlecategories on tbarticlecategories.articleid =  tbarticles.articleid
INNER JOIN tbcategories ON tbcategories.catid = tbarticlecategories.catid


where tbarticles.deletitem='1'   AND (tbcategories.parentid ='1' or  tbcategories.parentid ='2' or tbcategories.parentid ='3')


	  ORDER BY tbarticles.articleid desc
LIMIT 8
";

$result_artandculture = mysql_query($query_artandculture);
?>

Open in new window


FUNCTION

function doArticle_image300_artandculture($articleid,$title,$description1,$photo,$parentid,$catid,$mediastate,$altdescription) {
{
    // CREATE A RETURN STRING IN HEREDOC NOTATION
      $tab = <<<EOD
    <div class="wrapper-for-datapakage-firstpage">
      <div class="articletitle"><a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title 
		   </strong></a></div>
      <div class="row2-main-datapakage"><a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
		   <img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/></div>
      <div class="row3-main-datapakage">$description1...</div><hr class='hr95'></div>
    </div>
EOD;

    return $tab;
}
		  
          }
          $tab = "";
}

Open in new window


I want that arrange each block of data (call function) as 2 columns and in each row 2 blocks .

Thanks
0
MOSTAGHASSI
Asked:
MOSTAGHASSI
  • 3
  • 2
  • 2
1 Solution
 
Kim WalkerWeb Programmer/TechnicianCommented:
I'm not sure I understand the question. But your function code contains some mismatched curly braces. Line 1 ends with a begin curly brace and then line 2 contains another begin curly brace. Then lines 15, 17, and 19 each begin with an end curly brace. So that section of code should throw an error when parsed.

The snippet of code you labeled query appears to perform a database query and store the results in the variable $result_artandculture. Are you asking how to get the results of your query into the function?

I want that arrange each block of data (call function) as 2 columns and in each row 2 blocks .
This sounds as if you want to separate each block of data into 2 columns and arrange two blocks side by side for 4 columns on each row. Is this correct? What are you referring to as a block of data? Is that one row in the query result? The HTML code in your function appears to divide the data into three divs which all are direct descendants of the same parent div. If you wish to arrange these divs side-by-side, you need to show us your CSS for these elements.
0
 
MOSTAGHASSIAuthor Commented:
Thanks,sorry for mistake the  braces,it has happen during copy past,and forget it the function work well,

Are you asking how to get the results of your query into the function?

No.

This sounds as if you want to separate each block of data into 2 columns and arrange two blocks side by side for 4 columns on each row. Is this correct?

Yes and no.
the arrangement are like:

block1      block2

block3      block4

block5      block6

block7      block8

What are you referring to as a block of data?

Data block is 'function' that  contain 3 div(1-for title with a link-2 for image for the same link-3 small description for article).
0
 
Kim WalkerWeb Programmer/TechnicianCommented:
I'm sorry. I still don't understand what you're asking for. Are you asking how to arrange your blocks of data in two columns? Are your blocks of data contained in a parent div with class wrapper-for-datapakage-firstpage?

If so, arranging the parent divs side by side in two columns is done with CSS, not PHP. This can be accomplished by wrapping all the wrapper-for-datapakage-firstpage divs in another div (if they aren't already) which is wide enough for two, and then floating all the wrapper-for-datapakage-firstpage divs.

Executing your doArticle_image300_artandculture function eight times would generating the eight blocks of data within the two-columns div below.
<div class="two_columns">
	<!-- Block 1 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 2 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 3 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 4 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 5 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 6 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 7 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
	<!-- Block 8 (generated by function doArticle_image300_artandculture) -->
	<div class="wrapper-for-datapakage-firstpage">
		<div class="articletitle">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'><strong>$title</strong></a>
		</div>
		<div class="row2-main-datapakage">
			<a href='./$folder/$file?articleid=$articleid&parentid=$parentid&catid=$catid'>
			<img src='./images/mimage/$photo' border='0' alt='$altdescription' /></a><br/>
		</div>
		<div class="row3-main-datapakage">$description1...</div>
		<hr class='hr95'>
	</div>
</div>

Open in new window

The CSS to place them side by side would look something like
div.two_columns { width: 600px; height: auto; overflow: hidden; }
div.wrapper-for-datapakage-firstpage { width: 300px; margin: 0; padding: 0; float: left; }

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Ray PaseurCommented:
Couple of things.  First and most important: Get off MySQL.  Choose MySQLi  or choose PDO.  Here's the background telling why I recommend you get off MySQL as soon as possible, along with code examples showing how to make the change.  In my experience, object-oriented MySQLi  is the easiest conversion from MySQL.
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Second, you might want to take this question under consideration as a Model-View-Controller design pattern that uses a template, probably a HEREDOC, to build the HTML view of this data.  To do this, first write your HTML template, using PHP variables in the places that make sense, something like this:
<div><span>$block1</span><span>$block2</span></div>
<div><span>$block3</span><span>$block4</span></div>
<!-- etc... -->

Open in new window

Next, write your PHP code to assign the values to $block1, $block2, etc.  Test to verify that the correct data is getting put into the correct PHP variables and that these variables are getting injected into the HTML template.

Finally, add appropriate selector attributes (id, class) to the HTML tags, and write your CSS (and/or JavaScript) to cause the styling you need.

Information on PHP HEREDOC notation is available here.  Heed, but do not be put off by their "over-the-top" warning.  This is a really helpful feature of the language!
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
0
 
MOSTAGHASSIAuthor Commented:
Thanks Ray,specially for your great article, it seams that we must be ready for everyday upgrading,for this question i need your help,for convert this part of my old codes(works well) from 'table' to 'div' :

<?php
$result_artandculture = mysql_query($query_artandculture);

if ( $result_artandculture) {
	
	$tab = "<table width = 640  valign=top border=0 cellspacing=1 bordercolor=#ffffff bgcolor=#FFFFFF style=border: 1px solid #0066ff>\n";
	
	      $i = 1;
     for ( $i=1 ; $i<=4; $i++){
    $rw = @mysql_fetch_assoc( $result_artandculture);
     $tab .= "<tr>\n";
     $tab .= "<td   align=center bordercolor=#ffffff >" .doArticle_image300_artandculture( $rw['articleid'], $rw['title'],    $rw['description1'], $rw['photofilename'], $rw['parentid'], $rw['catid'],$rw['mediastate'],$rw['altdescription']) . "</td>\n"; 
	 
	 
	 if ( $rw = @mysql_fetch_assoc($result_artandculture ) )
	 $tab .= "<td   align=center bordercolor=#ffffff >" .doArticle_image300_artandculture( $rw['articleid'], $rw['title'], $rw['description1'], $rw['photofilename'], $rw['parentid'], $rw['catid'],$rw['mediastate'],$rw['altdescription']) . "</td>\n";		
	 
	 $tab .= "</tr>\n";
     }
	 $tab .= "</table>\n"  ; 
	} 
    echo   "$tab"; 
     ?>

Open in new window

0
 
Ray PaseurCommented:
This expert suggested creating a Gigs project.
This is not really a question with an answer -- it's more of a requirement for application development.  You might do well to try E-E Gigs.
0
 
MOSTAGHASSIAuthor Commented:
Please let me know that how much is normal for this very small project?
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now