Solved

Call custom function in php and arrange data as attach file

Posted on 2016-08-01
7
70 Views
Last Modified: 2016-08-05
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
Comment
Question by:MOSTAGHASSI
  • 3
  • 2
  • 2
7 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41738364
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
 

Author Comment

by:MOSTAGHASSI
ID: 41738494
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
 
LVL 22

Expert Comment

by:Kim Walker
ID: 41739736
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41742621
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
 

Author Comment

by:MOSTAGHASSI
ID: 41743203
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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41743278
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
 

Author Comment

by:MOSTAGHASSI
ID: 41744375
Please let me know that how much is normal for this very small project?
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ajax and PHP 4 47
How to get this library to work load? 8 40
$_GET call between URL 3 45
&& operators not performing required logic 8 18
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question